<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="霜序廿的个人网站">
  <link 
    rel="icon" 
    href="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg">
  <title>图论</title>
  
    
      <meta 
        property="og:title" 
        content="图论">
    
    
      <meta 
        property="og:url" 
        content="https://shuangxunian.github.io/2021/06/13/graphTheory/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img-blog.csdnimg.cn/20181119190833309.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21hamlubGVpMTIx,size_1,color_FFFFFF,t_70">
    
    
      <meta 
        property="og:img" 
        content="常用的图论题&amp;思路">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2021-06-13">
      <meta 
        property="og:article:modified_time" 
        content="2020-08-24">
      <meta 
        property="og:article:author" 
        content="霜序廿">
      
        
          <meta 
            property="og:article:tag" 
            content="图论">
        
      
    
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
      
      
      
      
        
        
        
        <script>
          function prismThemeChange() {
            if(document.getElementById('theme-color').dataset.mode === 'dark') {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism-tomorrow.min.css', '[data-prism]', 'prism-tomorrow');
              } else {
                loadCSS('/js/lib/prism/prism-tomorrow.min.css', 'prism', 'prism-tomorrow');
              }
            } else {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism.min.css', '[data-prism]', 'prism');
              } else {
                loadCSS('/js/lib/prism/prism.min.css', 'prism', 'prism');
              }
            }
          }
          prismThemeChange()
        </script>
      
      
        
        <link rel="stylesheet" href="/js/lib/prism/prism-line-numbers.min.css">
      
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
        prismThemeChange();
      }
    };
    setDarkmode();
    </script>
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
<meta name="generator" content="Hexo 5.4.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
        <img 
          class="navbar-logo-img" 
          src="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg" 
          alt="blog logo">
      
      <span class="navbar-logo-dsc">霜序廿的个人网站</span>
    </span>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <a 
      class="navbar-menu-item darknavbar" 
      id="dark">
      <i class="iconfont icon-weather"></i>
    </a>
    <a 
      class="navbar-menu-item searchnavbar" 
      id="search">
      <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i>
    </a>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <main class="main-column">
  <div class="image-wrapper">
    <img 
      src="https://img-blog.csdnimg.cn/20181119190833309.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21hamlubGVpMTIx,size_1,color_FFFFFF,t_70" 
      data-src="https://img-blog.csdnimg.cn/20181119190833309.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21hamlubGVpMTIx,size_1,color_FFFFFF,t_70"
      srcset="data:image/svg+xml,%3Csvg%20xmlns=&#39;http://www.w3.org/2000/svg&#39;%20viewBox=&#39;0%200%20300%20300&#39;%3E%3C/svg%3E"
      class="image lozad"
      alt="图论 thumbnail">
  </div>

<article class="card card-content">
  <header>
    <h1 class="post-title">
      图论
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2021-06-13T13:48:56.073Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2021-06-13</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/%E7%AE%97%E6%B3%95/" 
          class="post-meta-link">
          算法
        </a>
      
    
    
      <span class="dot"></span>
      <span>16.6k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/%E5%9B%BE%E8%AE%BA/" 
            class="post-meta-link">
            图论
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <h2 id="图论"><a href="#图论" class="headerlink" title="图论"></a>图论</h2><p>作者 Overstars</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Miaplacidus">https://blog.csdn.net/Miaplacidus</a></p>
<h3 id="常见概念"><a href="#常见概念" class="headerlink" title="常见概念"></a>常见概念</h3><ul>
<li><p>简单图：不含有平行边和环的图。</p>
</li>
<li><p>多重图：含有平行边（同向）的图。</p>
</li>
<li><p>完全图：每一对节点之间都有边的简单图，$n$个节点无向完全图记为$K_n$。</p>
</li>
<li><p>平面图：能将所有点和边画在平面上，且边与边不相交的<strong>无向</strong>图。</p>
</li>
<li><p>补图：由$G$中<strong>所有节点</strong>和所有能使$G$成为完全图的<strong>添加边</strong>组成的图。</p>
</li>
<li><p>生成子图(Spanning Subgraph)：含有原图$G$中所有结点的子图。</p>
</li>
<li><p>图同构的<strong>必要条件</strong>：</p>
<ol>
<li>节点数相同。</li>
<li>边数相同。</li>
<li>度数相同的结点数相同。<h4 id="网络流常用概念"><a href="#网络流常用概念" class="headerlink" title="网络流常用概念"></a>网络流常用概念</h4></li>
</ol>
</li>
<li><p>连通图：只有一个连通分支（极大连通子图）的图。</p>
</li>
<li><p>割点：无向连通图中一个顶点$v$,，若删除它以及关联的边后，得到的新图至少包含两个连通分支。</p>
</li>
<li><p>桥：无向连通图中的一条边$e$，删除它得到的新图包含两个连通分量。</p>
</li>
<li><p>团(Clique)：原图$G$的一个<strong>完全</strong>子图。</p>
</li>
<li><p>极大团(Maximal Clique)：不是其它团的子图的团。</p>
</li>
<li><p>最大团(Maximum Clique)：顶点最多的极大团。</p>
</li>
<li><p>诱导子图(Induced Subgraph)：所有节点在原图$G$上连接的边都被包含在内的子图。</p>
</li>
<li><p>独立集：在图上选出一些节点，这些<strong>节点间两两无边</strong>的点集。</p>
</li>
<li><p>路径覆盖：在图中找一些路径，这些路径覆盖图中所有的顶点，每个顶点都只与一条路径相关联。</p>
</li>
<li><p>最小染色数：用最少的颜色个数给点染色且任意两点相邻点颜色不同，最少的颜色个数。</p>
<h4 id="弦图常用概念"><a href="#弦图常用概念" class="headerlink" title="弦图常用概念"></a>弦图常用概念</h4></li>
<li><p>弦：连接环上两个不相邻节点的边。</p>
</li>
<li><p>弦图：图上任意一个点数大于三的环都至少存在一条弦的无向图。</p>
</li>
<li><p>单纯点：节点$v$与相邻节点的诱导子图是一个团。</p>
</li>
<li><p>完美消除序列：有$n$个点的排列$v_1,v_2,\dots,v_n$，该排列满足$v_i$在${v_i,v_{i+1},\dots,v_n}$的诱导子图中是一个单纯点。</p>
</li>
<li><p><strong>一个无向图是弦图当且仅当它有一个完美消除序列</strong></p>
</li>
</ul>
<h3 id="常见结论"><a href="#常见结论" class="headerlink" title="常见结论"></a>常见结论</h3><ul>
<li>每个图中节点<strong>度数和</strong>等于边数的二倍，$\sum\limits_{v\in V} deg(v)=2\left| E \right|$。</li>
<li>任何图中度数为奇数的节点必定有偶数个。</li>
<li>完全图$K_n$的边数$=\frac{n(n-1)}{2}$</li>
<li>最大团中顶点数量 = 补图的最大独立集中顶点数量</li>
<li>最大团数 ≤ 最小染色数，最大独立集 ≤ 最小团覆盖</li>
<li>弦图中：最大团数 = 最小染色数，最大独立集 = 最小团覆盖</li>
<li>平面图边数$m\le 3n-6$。</li>
<li>git config –global core.autocrlf false和git config –global core.safecrlf true还有git init</li>
</ul>
<h3 id="弦图"><a href="#弦图" class="headerlink" title="弦图"></a>弦图</h3><h4 id="最大势算法求消除序列并判定弦图"><a href="#最大势算法求消除序列并判定弦图" class="headerlink" title="最大势算法求消除序列并判定弦图"></a>最大势算法求消除序列并判定弦图</h4><p>判断一个消除序列是否为完美消除序列：从后向前枚举序列中的点$v_i$，设序列中在$v_i$后且与$v_i$相邻的点集<strong>依次</strong>为${v_{j1},v_{j2},\dots,v_{jk}}$，判断$v_j1$是否与${v_{j2},\dots,v_{jk}}$相邻即可。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">1005</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">edge</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> v<span class="token punctuation">,</span>nex<span class="token punctuation">;</span>
	<span class="token function">edge</span><span class="token punctuation">(</span><span class="token keyword">int</span> v<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">:</span>
		<span class="token function">v</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">nex</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> e<span class="token punctuation">[</span>maxn<span class="token operator">*</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>head<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> v<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	e<span class="token punctuation">[</span><span class="token operator">++</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">=</span>v<span class="token punctuation">;</span>
	e<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token operator">=</span>head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span>cnt<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> label<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>id<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>order<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//id[i]表示节点i在序列中的编号</span>
<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//order[i]为完美消除序列第i个节点,label[x]表示x点与多少个已标号节点相邻</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> vec<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//vec[x]存储*与x个已标号节点相邻*的节点</span>
<span class="token keyword">void</span> <span class="token function">mcs</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token comment">//节点数量</span>
<span class="token punctuation">&#123;</span><span class="token comment">//复杂度O(n+m)</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>label<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		vec<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> maxx<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>now<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//从前往后,每轮必定会找出一个</span>
		<span class="token keyword">bool</span> flag<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>flag<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>vec<span class="token punctuation">[</span>maxx<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">>=</span><span class="token number">0</span><span class="token punctuation">;</span>j<span class="token operator">--</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span><span class="token comment">//从后往前找</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>vec<span class="token punctuation">[</span>maxx<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//该节点已经标记过</span>
					vec<span class="token punctuation">[</span>maxx<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					flag<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//找到个未访问的节点</span>
					now<span class="token operator">=</span>vec<span class="token punctuation">[</span>maxx<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
					<span class="token keyword">break</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>flag<span class="token punctuation">)</span>
				maxx<span class="token operator">--</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		vis<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//</span>
		order<span class="token punctuation">[</span>n<span class="token operator">-</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span>now<span class="token punctuation">;</span>
		id<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">=</span>n<span class="token operator">-</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//节点x在序列中的位置</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>j<span class="token punctuation">;</span>j<span class="token operator">=</span>e<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//遍历与now节点相邻的节点</span>
			<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				label<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//v节点与已标号节点数++</span>
				vec<span class="token punctuation">[</span>label<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
				maxx<span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>maxx<span class="token punctuation">,</span>label<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//找出最大的那个节点</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> bucket<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">isperfect</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//复杂度O(n+m)</span>
	<span class="token function">mcs</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//计算消除序列并判断是否为完美消除序列</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//判断序列中的点v_i是否与其后所有点相接</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>n<span class="token punctuation">;</span>i<span class="token operator">></span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">--</span><span class="token punctuation">)</span><span class="token comment">//序列从后向前</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> tot<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>ret<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//每轮桶清空</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>head<span class="token punctuation">[</span>order<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>j<span class="token punctuation">;</span>j<span class="token operator">=</span>e<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>id<span class="token punctuation">[</span>e<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token operator">></span>i<span class="token punctuation">)</span><span class="token comment">//排在序列编号x后面与x相邻的点集记为:N(x)</span>
				vis<span class="token punctuation">[</span>bucket<span class="token punctuation">[</span><span class="token operator">++</span>tot<span class="token punctuation">]</span><span class="token operator">=</span>e<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//序列中x后且与x邻接点标记并放入桶中</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>head<span class="token punctuation">[</span>bucket<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>j<span class="token punctuation">;</span>j<span class="token operator">=</span>e<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span><span class="token comment">//buc[1]的id为N(x)中最小？</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//bucket[1]=0...</span>
			<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">!=</span>bucket<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span><span class="token comment">//判断N(x)中排在最前面的点是否与N(x)其他点相邻</span>
				ret<span class="token operator">++</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>tot<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>
			vis<span class="token punctuation">[</span>bucket<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//防止每次memset超时</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>tot<span class="token operator">&amp;&amp;</span>ret<span class="token operator">!=</span>tot<span class="token punctuation">)</span><span class="token comment">//不全部邻接</span>
			<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">,</span>u<span class="token punctuation">,</span>v<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>cin<span class="token operator">>></span>n<span class="token operator">>></span>m<span class="token operator">&amp;&amp;</span>n<span class="token operator">&amp;&amp;</span>m<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>head<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>head<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>order<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
			vec<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			cin<span class="token operator">>></span>u<span class="token operator">>></span>v<span class="token punctuation">;</span>
			<span class="token function">add</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">add</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		cout<span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span><span class="token function">isperfect</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token operator">?</span><span class="token string">"Perfect\n\n"</span><span class="token operator">:</span><span class="token string">"Imperfect\n\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="三元环"><a href="#三元环" class="headerlink" title="三元环"></a>三元环</h3><h4 id="步骤"><a href="#步骤" class="headerlink" title="步骤"></a>步骤</h4><ol>
<li>对原无向图进行定向，对任何一条边，从度数大的点连向度数小的点，如果度数相同，从编号小的点连向编号大的点。得到一个有向无环图。</li>
<li>枚举每一个节点$u$，将$u$所有相邻节点打上$u$的标记。再枚举$u$的相邻节点$v$，枚举$v$的相邻节点的相邻节点$w$，如果$w$上有被标记为$u$，则$(u,v,w)$就是一个三元环。</li>
</ol>
<p>统计图上三元环数量，复杂度$O(m\sqrt{m})$。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">vector G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//新图</span>
<span class="token keyword">int</span> vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>deg<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">cycle3</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
    <span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> <span class="token operator">&amp;</span>e<span class="token operator">:</span>edges<span class="token punctuation">)</span>
    <span class="token punctuation">&#123;</span><span class="token comment">//统计原无向图度数</span>
        deg<span class="token punctuation">[</span>e<span class="token punctuation">.</span>u<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
        deg<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> <span class="token operator">&amp;</span>e<span class="token operator">:</span>edges<span class="token punctuation">)</span><span class="token comment">//建立新图</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>deg<span class="token punctuation">[</span>e<span class="token punctuation">.</span>u<span class="token punctuation">]</span><span class="token operator">&lt;</span>deg<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token operator">||</span><span class="token punctuation">(</span>deg<span class="token punctuation">[</span>e<span class="token punctuation">.</span>u<span class="token punctuation">]</span><span class="token operator">==</span>deg<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>e<span class="token punctuation">.</span>u<span class="token operator">&lt;</span>e<span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">)</span>
			G<span class="token punctuation">[</span>e<span class="token punctuation">.</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">edge</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>u<span class="token punctuation">,</span>e<span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span>
			G<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">edge</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>v<span class="token punctuation">,</span>e<span class="token punctuation">.</span>u<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>u<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>u<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">&#123;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> v<span class="token operator">:</span>G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span>
            vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>u<span class="token punctuation">;</span><span class="token comment">//相邻节点打上u的标记</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> v<span class="token operator">:</span>G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> w<span class="token operator">:</span>G<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>w<span class="token punctuation">]</span><span class="token operator">==</span>u<span class="token punctuation">)</span>
                    ans<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="最短路"><a href="#最短路" class="headerlink" title="最短路"></a>最短路</h3><h4 id="Dijkstra堆优化"><a href="#Dijkstra堆优化" class="headerlink" title="Dijkstra堆优化"></a>Dijkstra堆优化</h4><p>复杂度$O(ElgE)$，稠密图中有时不如普通版优秀，但比SPFA快。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> MAXN<span class="token operator">=</span><span class="token number">1050</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">qnode</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> v<span class="token punctuation">,</span>c<span class="token punctuation">;</span>
	<span class="token function">qnode</span><span class="token punctuation">(</span><span class="token keyword">int</span> _v<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">int</span> _c<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">v</span><span class="token punctuation">(</span>_v<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">c</span><span class="token punctuation">(</span>_c<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token keyword">bool</span> <span class="token keyword">operator</span> <span class="token operator">&lt;</span><span class="token punctuation">(</span><span class="token keyword">const</span> qnode <span class="token operator">&amp;</span>r<span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">return</span> c<span class="token operator">></span>r<span class="token punctuation">.</span>c<span class="token punctuation">;</span><span class="token comment">//重载运算符&lt;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">Edge</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> v<span class="token punctuation">,</span>cost<span class="token punctuation">;</span>
	<span class="token function">Edge</span><span class="token punctuation">(</span><span class="token keyword">int</span> _v<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">int</span> _cost<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">v</span><span class="token punctuation">(</span>_v<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">cost</span><span class="token punctuation">(</span>_cost<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span>Edge<span class="token operator">></span>E<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//使用前必须清空0~n</span>
<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> dist<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//到这个点的最近队员的</span>
<span class="token keyword">void</span> <span class="token function">Dijkstra</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> start<span class="token punctuation">)</span><span class="token comment">//传入总数及起点</span>
<span class="token punctuation">&#123;</span><span class="token comment">//点的编号从 1 开始</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		dist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>inf<span class="token punctuation">;</span>
	priority_queue<span class="token operator">&lt;</span>qnode<span class="token operator">></span>que<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>que<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
		que<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	dist<span class="token punctuation">[</span>start<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	que<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">qnode</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>que<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		qnode now<span class="token operator">=</span>que<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		que<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> u<span class="token operator">=</span>now<span class="token punctuation">.</span>v<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token keyword">continue</span><span class="token punctuation">;</span>
		vis<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>E<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> v<span class="token operator">=</span>E<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			<span class="token keyword">int</span> cost<span class="token operator">=</span>E<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>cost<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">></span>dist<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">+</span>cost<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>dist<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">+</span>cost<span class="token punctuation">;</span>
				que<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">qnode</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">addedge</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> v<span class="token punctuation">,</span><span class="token keyword">int</span> w<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	E<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Edge</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="SPFA"><a href="#SPFA" class="headerlink" title="SPFA"></a>SPFA</h4><p>最坏复杂度$O(VE)$，V为节点数，不适用于稠密图</p>
<p>检测负环：当存在一个点入队大于等于$V$次，则有负环</p>
<h5 id="BFS实现（求最短路）"><a href="#BFS实现（求最短路）" class="headerlink" title="BFS实现（求最短路）"></a>BFS实现（求最短路）</h5><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> inf<span class="token operator">=</span><span class="token number">0x3f3f3f3f</span><span class="token punctuation">,</span>MAXN<span class="token operator">=</span><span class="token number">5051</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">node</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> v<span class="token punctuation">,</span>w<span class="token punctuation">,</span>next<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> e<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> cnt<span class="token punctuation">,</span>dist<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">,</span>head<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">,</span>num<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> v<span class="token punctuation">,</span><span class="token keyword">int</span> w<span class="token punctuation">)</span><span class="token comment">//链式前向星存图,无向则双向添加</span>
<span class="token punctuation">&#123;</span>
	e<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">=</span>v<span class="token punctuation">;</span><span class="token comment">//边的结尾节点</span>
	e<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token operator">=</span>w<span class="token punctuation">;</span>
	e<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>next<span class="token operator">=</span>head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//去找以u为起始的上一个节点(相当于链表,起始为-1)</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span>cnt<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//保存该边(最后的边)在e[i]中的编号</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">bool</span> <span class="token function">SPFA</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token comment">//节点数量n,起点编号x</span>
<span class="token punctuation">&#123;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span>inf<span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>dist<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	dist<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//该题起点任意</span>
	num<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//入队次数++</span>
	queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> QAQ<span class="token punctuation">;</span>
	QAQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>QAQ<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> now<span class="token operator">=</span>QAQ<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		QAQ<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		vis<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//从队列中取出</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token punctuation">;</span>i<span class="token operator">!=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>next<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//遍历以now开头的所有边,尝试以x->now->to松弛x->to</span>
			<span class="token keyword">int</span> to<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span><span class="token comment">//尝试松弛x->to的当前距离</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>dist<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token operator">></span>dist<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">+</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				dist<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token operator">=</span>dist<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">+</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span><span class="token comment">//成功用x->now->to松弛x->to</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//x->to被成功松弛且to不在队列</span>
				<span class="token punctuation">&#123;</span>
					vis<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//标记to加入队列</span>
					QAQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>to<span class="token punctuation">)</span><span class="token punctuation">;</span>
					num<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//to加入队列次数++</span>
					<span class="token keyword">if</span><span class="token punctuation">(</span>num<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token operator">></span>n<span class="token punctuation">)</span>
						<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//有负权回路,无法求最短路径</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="DFS优化（负环检测）"><a href="#DFS优化（负环检测）" class="headerlink" title="DFS优化（负环检测）"></a>DFS优化（负环检测）</h5><p>请先用前向星加边。因为图有可能不连通，检测负环要枚举每个起点。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">bool</span> <span class="token function">spfa</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token comment">//DFS优化</span>
<span class="token punctuation">&#123;</span>
	vis<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>next<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">></span>dist<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">)</span><span class="token comment">//求最短路</span>
		<span class="token punctuation">&#123;</span>
			dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>dist<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//存在一点在一条路径上出现多次，存在负权环</span>
				<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">spfa</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//搜到了存在负权环</span>
				<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	vis<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//未找到负权环</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="Floyd"><a href="#Floyd" class="headerlink" title="Floyd"></a>Floyd</h4><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">long</span> <span class="token keyword">long</span> path<span class="token punctuation">[</span><span class="token number">805</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">805</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">floyd</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token comment">//节点编号1~n</span>
<span class="token punctuation">&#123;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>k<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>k<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
            <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token operator">+</span>path<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">&lt;</span>path<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token punctuation">&#123;</span><span class="token comment">//当i，j的原来的边的最短距离，大于经过k点所到达的距离就替换</span>
                    path<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>path<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token operator">+</span>path<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="最大团"><a href="#最大团" class="headerlink" title="最大团"></a>最大团</h3><h4 id="最大团大小"><a href="#最大团大小" class="headerlink" title="最大团大小"></a><a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P4212">最大团大小</a></h4><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">60</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>tmp<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//tmp[i][j]搜到第i层</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span>ans<span class="token punctuation">,</span>tot<span class="token punctuation">,</span>f<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> dep<span class="token punctuation">,</span><span class="token keyword">int</span> num<span class="token punctuation">)</span><span class="token comment">//深度(团大小),备选集合</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>num<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token operator">></span>ans<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			ans<span class="token operator">=</span>dep<span class="token punctuation">;</span>
			<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>num<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token operator">+</span>num<span class="token operator">-</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token operator">&lt;=</span>ans<span class="token punctuation">)</span>
			<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>tmp<span class="token punctuation">[</span>dep<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>dep<span class="token operator">+</span>f<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">&lt;=</span>ans<span class="token punctuation">)</span>
			<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>num<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> vv<span class="token operator">=</span>tmp<span class="token punctuation">[</span>dep<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">[</span>vv<span class="token punctuation">]</span><span class="token punctuation">)</span>
				tmp<span class="token punctuation">[</span>dep<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">++</span>cnt<span class="token punctuation">]</span><span class="token operator">=</span>vv<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">dfs</span><span class="token punctuation">(</span>dep<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>cnt<span class="token punctuation">)</span><span class="token punctuation">)</span>
			<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">maxClique</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>n<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	f<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token operator">=</span>ans<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>n<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">>=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		tot<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span>
				tmp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">++</span>tot<span class="token punctuation">]</span><span class="token operator">=</span>j<span class="token punctuation">;</span>
		<span class="token function">dfs</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>tot<span class="token punctuation">)</span><span class="token punctuation">;</span>
		f<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>ans<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//回溯法求最大团</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> u<span class="token punctuation">,</span>v<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">~</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>u<span class="token punctuation">,</span><span class="token operator">&amp;</span>v<span class="token punctuation">)</span><span class="token punctuation">)</span>
		G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>G<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span><span class="token function">maxClique</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="差分约束"><a href="#差分约束" class="headerlink" title="差分约束"></a>差分约束</h3><h4 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h4><p>如果一个系统是由n个变量和<strong>m个约束条件</strong>组成，并且每个约束条件能够形成形如$x_i−x_j\le c_k$的形式，我们就称该系统为差分约束系统。</p>
<h4 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h4><p>先将不等号方向统一，统一为如上形式时，从$x_j$向$x_i$连一条权值为$c_k$的边。</p>
<h3 id="最小生成树-MST"><a href="#最小生成树-MST" class="headerlink" title="最小生成树(MST)"></a>最小生成树(MST)</h3><h4 id="Prim算法"><a href="#Prim算法" class="headerlink" title="Prim算法"></a>Prim算法</h4><p>复杂度：邻接矩阵:$O(V^2)$，邻接表:$O(ElogV)$</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> inf<span class="token operator">=</span><span class="token number">0x3f3f3f3f</span><span class="token punctuation">,</span>maxn<span class="token operator">=</span><span class="token number">105</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> dist<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>closest<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>lowcost<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> tree<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">prim</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> u0<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>tree<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>tree<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	tree<span class="token punctuation">[</span>u0<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//加入树</span>
	<span class="token keyword">int</span> i<span class="token punctuation">,</span>j<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//① 初始化,</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">!=</span>u0<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			lowcost<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>dist<span class="token punctuation">[</span>u0<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
			closest<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>u0<span class="token punctuation">;</span><span class="token comment">//一开始只有u0</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">else</span>
			lowcost<span class="token punctuation">[</span>u0<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//② 每次选出来一个最近的节点</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> temp<span class="token operator">=</span>inf<span class="token punctuation">,</span>t<span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span>j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//③ 在V-u中寻找最近的节点t</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>tree<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>lowcost<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">&lt;</span>temp<span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				t<span class="token operator">=</span>j<span class="token punctuation">;</span>
				temp<span class="token operator">=</span>lowcost<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>t<span class="token operator">==</span>u0<span class="token punctuation">)</span><span class="token comment">//找不到t,没有可加入的节点,跳出</span>
			<span class="token keyword">break</span><span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//找到了就加入tree</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span>j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//④ 根据加入的t节点更新lowcost和closest</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>tree<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">&lt;</span>lowcost<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				lowcost<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
				closest<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>t<span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		ans<span class="token operator">+=</span>lowcost<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="Kruskal"><a href="#Kruskal" class="headerlink" title="Kruskal"></a>Kruskal</h4><p>使用并查集优化，复杂度$O(mlogm)$</p>
<p>这个不能忘吧……就先不贴了</p>
<h4 id="Boruvka"><a href="#Boruvka" class="headerlink" title="Boruvka"></a>Boruvka</h4><p>因为没有$kruskal$好写，所以一般不用于MST裸题。</p>
<p>适于处理<strong>边权由连接的两个点的点权通过某种计算方式得出</strong>的情况。</p>
<p>平均 $O(V+E)$，最坏 $O((V+E)logV)$。 </p>
<h5 id="流程"><a href="#流程" class="headerlink" title="流程"></a>流程</h5><ol>
<li><p>对每个连通块，处理出与其他连通块连接的最小代价，并记录这条边。</p>
</li>
<li><p>连接所有连通块与其最小连接代价的连通块，并将该边边权计入。</p>
</li>
<li><p>若剩余连通块数量大于1，重复上述步骤。</p>
</li>
</ol>
<h5 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h5><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">struct</span> <span class="token class-name">edge</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> u<span class="token punctuation">,</span>v<span class="token punctuation">,</span>w<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span>edge<span class="token operator">></span> E<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">boruvka</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//n个连通块</span>
		fa<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>
	<span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">cost</span><span class="token punctuation">(</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">rec</span><span class="token punctuation">(</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	vector<span class="token operator">&lt;</span><span class="token keyword">bool</span><span class="token operator">></span> <span class="token function">vis</span><span class="token punctuation">(</span>E<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
			cost<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>inf<span class="token punctuation">;</span><span class="token comment">//初始化为inf</span>
		<span class="token keyword">int</span> cur<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//统计不同连通块</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>E<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> a<span class="token operator">=</span><span class="token function">findfa</span><span class="token punctuation">(</span>E<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">)</span><span class="token punctuation">,</span>b<span class="token operator">=</span><span class="token function">findfa</span><span class="token punctuation">(</span>E<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">,</span>w<span class="token operator">=</span>E<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>a<span class="token operator">==</span>b<span class="token punctuation">)</span>
				<span class="token keyword">continue</span><span class="token punctuation">;</span>
			cur<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//记录a,b两个连通块连接的最小代价</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>w<span class="token operator">&lt;</span>cost<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">)</span>
				cost<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token operator">=</span>w<span class="token punctuation">,</span>rec<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span><span class="token comment">//记录最小联通代价与相应边</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>w<span class="token operator">&lt;</span>cost<span class="token punctuation">[</span>b<span class="token punctuation">]</span><span class="token punctuation">)</span>
				cost<span class="token punctuation">[</span>b<span class="token punctuation">]</span><span class="token operator">=</span>w<span class="token punctuation">,</span>rec<span class="token punctuation">[</span>b<span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>cur<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span>
			<span class="token keyword">break</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//最坏情况是连接的连通块数目/2</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>cost<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">&lt;</span>inf<span class="token operator">&amp;&amp;</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>rec<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//与i相接的权值最小的边未加入</span>
			<span class="token punctuation">&#123;</span>
				<span class="token function">Merge</span><span class="token punctuation">(</span>E<span class="token punctuation">[</span>rec<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span>E<span class="token punctuation">[</span>rec<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//连接两个连通块</span>
				ans<span class="token operator">+=</span>E<span class="token punctuation">[</span>rec<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span>
				vis<span class="token punctuation">[</span>rec<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span><span class="token comment">//标记该边已加入,避免重复计算</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="典型例题"><a href="#典型例题" class="headerlink" title="典型例题"></a>典型例题</h4><ul>
<li><a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P1550">打井问题 </a>：将地下水源缩成一个点，添加到图中，建立MST。</li>
<li>无根MDST：建立超级源点s，向每一个节点连接一条值为INF的边，以s为根跑MDST，s的出边即为答案MDST的树根。</li>
<li><a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/CF888G">异或生成树</a>：字典树合并连通块</li>
</ul>
<h3 id="次小生成树"><a href="#次小生成树" class="headerlink" title="次小生成树"></a>次小生成树</h3><h4 id="严格次小生成树-洛谷P4180"><a href="#严格次小生成树-洛谷P4180" class="headerlink" title="严格次小生成树(洛谷P4180)"></a>严格次小生成树(洛谷P4180)</h4><p>建立最小生成树MST，倍增维护MST上任一点到LCA最大值与次大值。</p>
<p>记录树的重量val，并对边集中选中边进行标记。倍增算出每个节点到祖先的路径最大边与严格次大边权值。枚举每一条不在MST上的的边u-&gt;v(权值w)，分别计算新树上u-&gt;lca(u,v)与v-&gt;lca(u,v)的路径<strong>最大边权M</strong>与<strong>严格次大边权m</strong>，记录<strong>最小非零</strong>增量inc=w-M(当M==w时inc=w-m)。最后val+inc即为次小生成树重量。</p>
<p>复杂度瓶颈在排序，$O(mlogm)$</p>
<h3 id="有向图最小生成树（MDST，最小树形图）"><a href="#有向图最小生成树（MDST，最小树形图）" class="headerlink" title="有向图最小生成树（MDST，最小树形图）"></a>有向图最小生成树（MDST，最小树形图）</h3><ul>
<li><p>树形图：有向图$G=(V,E)$中，选定根节点$root$，$G$的一个以$root$为根节点的子图$T$，$T$中$root$到任意其他节点路径<strong>存在且唯一</strong>。则$T$称为有向图生成树/树形图DST。</p>
</li>
<li><p>最小树形图：带权有向图$G=(V,E,w)$中边权总和最小的DST，即Minimum Directed Spanning Trees。</p>
</li>
<li><p>特点：</p>
<ul>
<li>MDST上一定有且仅有$n-1$条边</li>
<li>根节点入度为0，其他节点入度为1<h4 id="朱刘算法（Edmonds-算法）"><a href="#朱刘算法（Edmonds-算法）" class="headerlink" title="朱刘算法（Edmonds 算法）"></a>朱刘算法（Edmonds 算法）</h4></li>
</ul>
</li>
</ul>
<h5 id="流程-1"><a href="#流程-1" class="headerlink" title="流程"></a>流程</h5><ol>
<li>对每个非根节点，找出权值最小的入边（n-1条），记为集合$E$。若$E$不存在，则MDST一定不存在。</li>
<li>判断E中是否成环，成环转到步骤3，否则转到步骤4。</li>
<li>若成环则进行<strong>缩点</strong>，同时更新指向该环的所有边的权值，此更新等效于删去环上的一条边。<ul>
<li>记该环为$C$，在新图$G_1$中收缩为点$u$，则对于在$G$图中不在环上的且指向该环上任一点$v$的一条边$&lt;v_1,v&gt;$，该边权值记为$w$，在$G_1$中存在边$&lt;v_1,u&gt;$与之对应，且该边权值$W_{G_1}(&lt;v_1,u&gt;)$=$W_{G}(&lt;v_1,v&gt;)-w$。</li>
<li>因为任何一个节点入度都不会大于1，在环$C$上已经为点$v$选择了一条入边，所以要根据改边权值更新其他点$v$入边权值，当接下来选择了其他指向$v$的边时，相当于删去了$C$上指向$v$的边。</li>
<li>转到步骤1，直到证明不存在或者求得。</li>
</ul>
</li>
<li>不成环则展开收缩点，获得MDST。若仅须获得MDST的权值，则不需要展开。<h5 id="模板"><a href="#模板" class="headerlink" title="模板"></a>模板</h5></li>
</ol>
<p>朱刘算法，不包括展开部分，未优化，复杂度$O(VE)$</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">1005</span><span class="token punctuation">,</span>inf<span class="token operator">=</span><span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">edge</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> u<span class="token punctuation">,</span>v<span class="token punctuation">,</span>w<span class="token punctuation">;</span>
	<span class="token function">edge</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> v<span class="token punctuation">,</span><span class="token keyword">int</span> w<span class="token punctuation">)</span><span class="token operator">:</span>
		<span class="token function">u</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">v</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">w</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span>edge<span class="token operator">></span> G<span class="token punctuation">;</span><span class="token comment">//该算法会修改边</span>
<span class="token keyword">int</span> id<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>in<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>pre<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//in[x]表示x入边最小权,pre[x]表示x最小入边的出点</span>
ll <span class="token function">zltree</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token comment">//id[x]为x节点在G图上的编号</span>
<span class="token punctuation">&#123;</span>
	ll ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token function">fill</span><span class="token punctuation">(</span>in<span class="token punctuation">,</span>in<span class="token operator">+</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>inf<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> <span class="token operator">&amp;</span>e<span class="token operator">:</span>G<span class="token punctuation">)</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>u<span class="token operator">!=</span>e<span class="token punctuation">.</span>v<span class="token operator">&amp;&amp;</span>e<span class="token punctuation">.</span>w<span class="token operator">&lt;</span>in<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				pre<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token operator">=</span>e<span class="token punctuation">.</span>u<span class="token punctuation">;</span><span class="token comment">//记录最小入边出点</span>
				in<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token operator">=</span>e<span class="token punctuation">.</span>w<span class="token punctuation">;</span><span class="token comment">//记录最小入边权</span>
			<span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">!=</span>root<span class="token operator">&amp;&amp;</span>in<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">==</span>inf<span class="token punctuation">)</span>
				<span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//存在非根点没有入边,无MDST</span>
		<span class="token function">fill</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span>id<span class="token operator">+</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> tn<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>v<span class="token punctuation">;</span><span class="token comment">//tn记录环的数量</span>
		in<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//根节点无入边,权为0(这样不用特判)</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//找环</span>
		<span class="token punctuation">&#123;</span>
			ans<span class="token operator">+=</span>in<span class="token punctuation">[</span>v<span class="token operator">=</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//加v入边贡献</span>
			<span class="token keyword">while</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">!=</span>i<span class="token operator">&amp;&amp;</span><span class="token operator">!</span>id<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>v<span class="token operator">!=</span>root<span class="token punctuation">)</span><span class="token comment">//</span>
				vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">,</span>v<span class="token operator">=</span>pre<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//检查v的最小入边出点,并标记vis为i</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">!=</span>root<span class="token operator">&amp;&amp;</span><span class="token operator">!</span>id<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				id<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>tn<span class="token punctuation">;</span><span class="token comment">//标记环的编号</span>
				<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token operator">=</span>pre<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span>u<span class="token operator">!=</span>v<span class="token punctuation">;</span>u<span class="token operator">=</span>pre<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span>
					id<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span>tn<span class="token punctuation">;</span><span class="token comment">//将v所在环打上同一个标记</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>tn<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">//无环</span>
			<span class="token keyword">break</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>id<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//给不在环上的点新编号</span>
				id<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>tn<span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>G<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">)</span><span class="token comment">//更新为新图G1</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">auto</span> <span class="token operator">&amp;</span>e<span class="token operator">=</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
			v<span class="token operator">=</span>e<span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			e<span class="token punctuation">.</span>u<span class="token operator">=</span>id<span class="token punctuation">[</span>e<span class="token punctuation">.</span>u<span class="token punctuation">]</span><span class="token punctuation">,</span>e<span class="token punctuation">.</span>v<span class="token operator">=</span>id<span class="token punctuation">[</span>e<span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>u<span class="token operator">!=</span>e<span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token comment">//更新指向环的边权</span>
				e<span class="token punctuation">.</span>w<span class="token operator">-=</span>in<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">,</span>i<span class="token operator">++</span><span class="token punctuation">;</span>
			<span class="token keyword">else</span>
			<span class="token punctuation">&#123;</span>
				<span class="token function">swap</span><span class="token punctuation">(</span>e<span class="token punctuation">,</span>G<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				G<span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		n<span class="token operator">=</span>tn<span class="token punctuation">;</span><span class="token comment">//更新新图的点数</span>
		root<span class="token operator">=</span>id<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//更新新图上根节点编号</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">,</span>r<span class="token punctuation">,</span>u<span class="token punctuation">,</span>v<span class="token punctuation">,</span>w<span class="token punctuation">;</span>
	cin<span class="token operator">>></span>n<span class="token operator">>></span>m<span class="token operator">>></span>r<span class="token punctuation">;</span><span class="token comment">//n节点,m条边,根节点r</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		cin<span class="token operator">>></span>u<span class="token operator">>></span>v<span class="token operator">>></span>w<span class="token punctuation">;</span>
		G<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">edge</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>v<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//有向</span>
	<span class="token punctuation">&#125;</span>
	cout<span class="token operator">&lt;&lt;</span><span class="token function">zltree</span><span class="token punctuation">(</span>r<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="虚树"><a href="#虚树" class="headerlink" title="虚树"></a>虚树</h3><p>虚树是在树形dp中使用的一种特殊优化，适用于树中仅有<strong>少量关键节点</strong>且普通节点很多的情况。可以将关键点和他们的LCA拿出来另建一棵树，并在这棵树上另外进行树形dp。</p>
<h4 id="步骤-1"><a href="#步骤-1" class="headerlink" title="步骤"></a>步骤</h4><ol>
<li>在原树上进行dfs，进行LCA预处理，同时得到原树上的dfs序，方便之后虚树构造，此外还可以进行一些dp预处理，便于进行虚树上的第二次dp。</li>
<li>确定关键节点集合，并按照dfs序排序。</li>
<li>通过单调栈及LCA算法构建出虚树。</li>
<li>在虚树上进行树形dp求解。<h4 id="代码示例"><a href="#代码示例" class="headerlink" title="代码示例"></a>代码示例</h4></li>
</ol>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token function">sort</span><span class="token punctuation">(</span>h<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>h<span class="token operator">+</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">int</span> <span class="token operator">&amp;</span>x<span class="token punctuation">,</span><span class="token keyword">const</span> <span class="token keyword">int</span> <span class="token operator">&amp;</span>y<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">return</span> dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">&lt;</span>dfn<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//按dfs序排序</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
stk<span class="token punctuation">[</span>top<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span>h<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
cnt2<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>k<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
    <span class="token keyword">int</span> now<span class="token operator">=</span>h<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> lc<span class="token operator">=</span><span class="token function">lca</span><span class="token punctuation">(</span>now<span class="token punctuation">,</span>stk<span class="token punctuation">[</span>top<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//最近公共祖先</span>
    <span class="token comment">//printf("lca(%d,%d)=%d\n",now,stk[top],lc);</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token operator">></span><span class="token number">1</span><span class="token operator">&amp;&amp;</span>dfn<span class="token punctuation">[</span>lc<span class="token punctuation">]</span><span class="token operator">&lt;=</span>dfn<span class="token punctuation">[</span>stk<span class="token punctuation">[</span>top<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//情况4,=是情况3</span>
    <span class="token punctuation">&#123;</span><span class="token comment">//不断将top送入虚树</span>
        <span class="token function">adde</span><span class="token punctuation">(</span>stk<span class="token punctuation">[</span>top<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>stk<span class="token punctuation">[</span>top<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//前向星加边,构建新树</span>
        top<span class="token operator">--</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>dfn<span class="token punctuation">[</span>lc<span class="token punctuation">]</span><span class="token operator">&lt;</span>dfn<span class="token punctuation">[</span>stk<span class="token punctuation">[</span>top<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//情况2</span>
    <span class="token punctuation">&#123;</span><span class="token comment">//加边,top出栈,lc和now入栈</span>
        <span class="token function">adde</span><span class="token punctuation">(</span>lc<span class="token punctuation">,</span>stk<span class="token punctuation">[</span>top<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stk<span class="token punctuation">[</span>top<span class="token punctuation">]</span><span class="token operator">=</span>lc<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span class="token comment">//否则为情况1</span>
    stk<span class="token punctuation">[</span><span class="token operator">++</span>top<span class="token punctuation">]</span><span class="token operator">=</span>now<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">--</span>top<span class="token punctuation">)</span>
    <span class="token function">adde</span><span class="token punctuation">(</span>stk<span class="token punctuation">[</span>top<span class="token punctuation">]</span><span class="token punctuation">,</span>stk<span class="token punctuation">[</span>top<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="拓扑排序"><a href="#拓扑排序" class="headerlink" title="拓扑排序"></a>拓扑排序</h3><p>只适用于DAG。</p>
<p>使用队列（要求字典序时使用优先队列），在邻接表存边时统计每个结点的入度，入度为0则入队。按出队顺序编号，删除以该节点为尾的边，该边边头<strong>入度减1</strong>，若变为0则入队。直到队列为空，得到top数组与node数组。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">105</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">,</span>top<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>node<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//节点i拓扑顺序为top[i]</span>
<span class="token keyword">void</span> <span class="token function">topsort</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//要求字典序：优先队列，小根堆</span>
	priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">,</span>greater<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">></span> QAQ<span class="token punctuation">;</span><span class="token comment">//若要求字典序用优先队列</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">//G[i][0]表示入度</span>
			QAQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>QAQ<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> x<span class="token operator">=</span>QAQ<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>cnt<span class="token punctuation">;</span><span class="token comment">//top[x]表示x号节点的次序编号</span>
		QAQ<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">--</span>G<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">//节点入度为0时入队</span>
				QAQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>cnt<span class="token operator">!=</span>n<span class="token punctuation">)</span><span class="token comment">//</span>
		<span class="token keyword">return</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		node<span class="token punctuation">[</span>top<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span><span class="token comment">//node[i]表示排序为i的节点为node[i]</span>
<span class="token comment">//	for(int i=1;i&lt;=n;i++)</span>
<span class="token comment">//		printf("%d%c",node[i],i&lt;n?' ':'\n');//输出拓扑排序后的节点</span>
	<span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>cin<span class="token operator">>></span>n<span class="token operator">>></span>m<span class="token operator">&amp;&amp;</span><span class="token punctuation">(</span>n<span class="token operator">||</span>m<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//注意清空</span>
			G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//G[i][0]用来统计节点i的入度</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> u<span class="token punctuation">,</span>v<span class="token punctuation">;</span>
			cin<span class="token operator">>></span>u<span class="token operator">>></span>v<span class="token punctuation">;</span>
			G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//向G中加边</span>
			G<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//v的入度+1</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">topsort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="补图搜索"><a href="#补图搜索" class="headerlink" title="补图搜索"></a>补图搜索</h3><p>也是很套路的题了，使用$set$数组来代替邻接表，用$set.find(v)$来确定边是否在图中。并查集、DFS、BFS都可以做。</p>
<h4 id="补图连通块-0-1-MST"><a href="#补图连通块-0-1-MST" class="headerlink" title="补图连通块 0-1 MST "></a>补图连通块 <a target="_blank" rel="noopener" href="https://codeforc.es/contest/1243/problem/D">0-1 MST </a></h4><p>初始化$ans=0$，并将所有的点放进一个未访问集合$unvis$中，当集合非空时，取出$unvis.begin()$记为$now$并从集合中去掉，并从该点开始BFS，遍历$nuvis$集，并在邻接表$set[now]$中查询，若$set.find()$未找到，则说明该边为补边。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">set<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//使用set存原图,加快速度</span>
<span class="token keyword">int</span> <span class="token function">bfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	set<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> unvis<span class="token punctuation">;</span><span class="token comment">//已访问节点,从原图上删除</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		unvis<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将所有点加入到nuvis集合中</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>unvis<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//BFS求连通块个数</span>
	<span class="token punctuation">&#123;</span>
		ans<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//从未访问集里拿出新的元素,新增一个连通块</span>
		<span class="token keyword">int</span> now<span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">(</span>unvis<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//取出第一个</span>
		unvis<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span>now<span class="token punctuation">)</span><span class="token punctuation">;</span>
		queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> QwQ<span class="token punctuation">;</span>
		QwQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>now<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>QwQ<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//找出与now联通的节点,并从unvis中删去</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> nex<span class="token operator">=</span>QwQ<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//与now联通的点之一</span>
			QwQ<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> v1<span class="token punctuation">;</span><span class="token comment">//记录要删去的节点</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> i<span class="token operator">:</span>unvis<span class="token punctuation">)</span><span class="token comment">//遍历未访问集合</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>nex<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token operator">==</span>G<span class="token punctuation">[</span>nex<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
				<span class="token punctuation">&#123;</span><span class="token comment">//now与i由补边连接,权重为0</span>
					v1<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
					QwQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//放进队列里,继续向下求联通点</span>
				<span class="token punctuation">&#125;</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>v1<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
				unvis<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span>v1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在集合中删去搜到的联通节点</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="强连通分量"><a href="#强连通分量" class="headerlink" title="强连通分量"></a>强连通分量</h3><h4 id="Tarjan"><a href="#Tarjan" class="headerlink" title="Tarjan"></a>Tarjan</h4><h5 id="Tarjan缩点-DAG-拓扑排序dp"><a href="#Tarjan缩点-DAG-拓扑排序dp" class="headerlink" title="Tarjan缩点+DAG 拓扑排序dp"></a>Tarjan缩点+DAG 拓扑排序dp</h5><p>n个点m条边的有向图，每个点有一个权值，求一条路径，使路径经过的点权值之和最大。</p>
<p>思路：使用<strong>Tarjan缩点</strong>建立新图DAG，在DAG上进行拓扑排序并进行DP。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">100005</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> tot<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>Index<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> stk<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>low<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>belong<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> val<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>rec<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//每个强连通分量的值</span>
<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//邻接表</span>
<span class="token keyword">void</span> <span class="token function">tarjan</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token comment">//标准的Tarjan缩点</span>
<span class="token punctuation">&#123;</span>
	dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>tim<span class="token punctuation">;</span><span class="token comment">//dfs序</span>
	stk<span class="token punctuation">[</span><span class="token operator">++</span>Index<span class="token punctuation">]</span><span class="token operator">=</span>x<span class="token punctuation">;</span>
	vis<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token operator">&amp;</span>v<span class="token operator">:</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//v未被访问</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">tarjan</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
			low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>low<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//回溯时更新low</span>
		<span class="token punctuation">&#125;</span><span class="token comment">//low[x]为x所在强连通分量最早起始节点</span>
		<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//v在栈中,说明有环</span>
			low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//更新起点为最早的那个</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">==</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//以x为起点的强连通分量</span>
		cnt<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//新图节点++</span>
		<span class="token keyword">do</span><span class="token punctuation">&#123;</span>
			belong<span class="token punctuation">[</span>stk<span class="token punctuation">[</span>Index<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span>cnt<span class="token punctuation">;</span>
			rec<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token operator">+=</span>val<span class="token punctuation">[</span>stk<span class="token punctuation">[</span>Index<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//缩点后的权值</span>
			vis<span class="token punctuation">[</span>stk<span class="token punctuation">[</span>Index<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
			Index<span class="token operator">--</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span><span class="token keyword">while</span><span class="token punctuation">(</span>stk<span class="token punctuation">[</span>Index<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">!=</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> Gra<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> dp<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>top<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>into<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">topsort</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> QAQ<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>cnt<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>into<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
			QAQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
		dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>rec<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> flag<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>QAQ<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> x<span class="token operator">=</span>QAQ<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		QAQ<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>flag<span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>Gra<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			dp<span class="token punctuation">[</span>Gra<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>dp<span class="token punctuation">[</span>Gra<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>dp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>rec<span class="token punctuation">[</span>Gra<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">--</span>into<span class="token punctuation">[</span>Gra<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span>
				QAQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>Gra<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>cnt<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		ans<span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span>dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">;</span>
	cin<span class="token operator">>></span>n<span class="token operator">>></span>m<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		cin<span class="token operator">>></span>val<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//每个点的值</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span>v<span class="token punctuation">;</span>
		cin<span class="token operator">>></span>u<span class="token operator">>></span>v<span class="token punctuation">;</span>
		G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//Tarjan缩点部分</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dfn<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token function">tarjan</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//缩点</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//拓扑排序部分</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>j<span class="token operator">&lt;</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> x<span class="token operator">=</span>belong<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>y<span class="token operator">=</span>belong<span class="token punctuation">[</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">!=</span>y<span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				Gra<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//建立新图DAG</span>
				into<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	cout<span class="token operator">&lt;&lt;</span><span class="token function">topsort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="Tarjan无向图求割点"><a href="#Tarjan无向图求割点" class="headerlink" title="Tarjan无向图求割点"></a>Tarjan无向图求割点</h5><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> tot<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>Index<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> dfn<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>low<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>stk<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>isc<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">tarjan</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> fa<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>tot<span class="token punctuation">;</span>
	<span class="token keyword">int</span> child<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">tarjan</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
			low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>low<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">==</span>fa<span class="token punctuation">)</span>
				child<span class="token operator">++</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">!=</span>fa<span class="token operator">&amp;&amp;</span>low<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">>=</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
				isc<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">!=</span>fa<span class="token punctuation">)</span><span class="token comment">//不同之处</span>
			low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">==</span>fa<span class="token operator">&amp;&amp;</span>child<span class="token operator">>=</span><span class="token number">2</span><span class="token punctuation">)</span>
		isc<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="无向图求桥"><a href="#无向图求桥" class="headerlink" title="无向图求桥"></a>无向图求桥</h5><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">void</span> <span class="token function">tarjan</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> fa<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>tot<span class="token punctuation">;</span>
	<span class="token keyword">bool</span> vis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//处理重边要加上,表示这个节点还没有被子树搜到</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span>no<span class="token operator">=</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>no<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">tarjan</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">></span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//讨论桥是大于</span>
			<span class="token punctuation">&#123;</span>
				bri<span class="token punctuation">[</span>no<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//法1，对桥的编号做标记</span>
<span class="token comment">//				pair&lt;int,int> tem;//法二，将桥存到新的数组中</span>
<span class="token comment">//				tem.first=x,tem.second=v;</span>
<span class="token comment">//				ans[flag++]=tem;</span>
			<span class="token punctuation">&#125;</span>
			low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>low<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//可改为无条件？</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">==</span>fa<span class="token operator">&amp;&amp;</span><span class="token operator">!</span>vis<span class="token punctuation">)</span>
				vis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//除了第一次，每次回到父节点都用父节点的值更新当前结点的值</span>
			<span class="token keyword">else</span><span class="token comment">//之前是v!=fa时才用父节点值更新该点的值</span>
				low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>low<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="2-SAT问题"><a href="#2-SAT问题" class="headerlink" title="2-SAT问题"></a>2-SAT问题</h4><h5 id="流程-2"><a href="#流程-2" class="headerlink" title="流程"></a>流程</h5><ol>
<li>将点$i$拆成$i$与$n+i$两个点，分别表示点$i$状态为$0$或$1$，二者必须且只能取其一。</li>
<li>根据所给逻辑关系建图，将$2n$个点进行缩点。</li>
<li>若存在一对拆点位于同一个强连通分量，则无解。</li>
<li>否则对于每个点对，选择<strong>分量编号</strong>较小的点（即拓扑序较大的那个）。</li>
</ol>
<h5 id="建图方式"><a href="#建图方式" class="headerlink" title="建图方式"></a>建图方式</h5><table>
<thead>
<tr>
<th>逻辑表达式</th>
<th>连接的有向边(推导关系)</th>
</tr>
</thead>
<tbody><tr>
<td>$a\land b=1$</td>
<td>$\lnot a\rightarrow a,\lnot b \rightarrow b$</td>
</tr>
<tr>
<td>$a\land b=0$</td>
<td>$a\rightarrow \lnot b,b \rightarrow \lnot a$</td>
</tr>
<tr>
<td>$a \lor b=1$</td>
<td>$\lnot a\rightarrow b,\lnot b \rightarrow a$</td>
</tr>
<tr>
<td>$a \lor b=0$</td>
<td>$a\rightarrow \lnot a,b \rightarrow \lnot b$</td>
</tr>
<tr>
<td>$a\oplus b=1$</td>
<td>$\lnot a\rightarrow b,b \rightarrow \lnot a,\lnot b \rightarrow a,a\rightarrow \lnot b$</td>
</tr>
<tr>
<td>$a\oplus b=0$，或$a\odot b=1$</td>
<td>$a\rightarrow b,b\rightarrow a,\lnot a\rightarrow \lnot b,\lnot b\rightarrow \lnot a$</td>
</tr>
<tr>
<td>$a\rightarrow b$</td>
<td>$a\rightarrow b,\lnot b\rightarrow\lnot a$</td>
</tr>
<tr>
<td>$\lnot a\rightarrow\lnot b$</td>
<td>$\lnot a\rightarrow\lnot b,b\rightarrow a$</td>
</tr>
</tbody></table>
<h3 id="最近公共祖先-LCA"><a href="#最近公共祖先-LCA" class="headerlink" title="最近公共祖先(LCA)"></a>最近公共祖先(LCA)</h3><p>因为LCA只适用于树，所以经常和生成树在一起考。</p>
<h4 id="倍增"><a href="#倍增" class="headerlink" title="倍增"></a>倍增</h4><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">100005</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxl<span class="token operator">=</span><span class="token number">30</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//无权边,也可以选择链式前向星存图</span>
<span class="token keyword">int</span> gene<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">[</span>maxl<span class="token punctuation">]</span><span class="token punctuation">,</span>depth<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>lg<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> nodes<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//子树结点的数量</span>
<span class="token keyword">void</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> fa<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>depth<span class="token punctuation">[</span>fa<span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
	gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span>fa<span class="token punctuation">;</span>
	nodes<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">&lt;&lt;</span>i<span class="token punctuation">)</span><span class="token operator">&lt;=</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//倍增</span>
		gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>gene<span class="token punctuation">[</span>gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">!=</span>fa<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">dfs</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在dfs前后加语句可以求出许多有趣的东西</span>
			nodes<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+=</span>nodes<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">lca</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">&lt;</span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//保证x深度≥y</span>
		<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//将x提到同一高度</span>
		x<span class="token operator">=</span>gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>lg<span class="token punctuation">[</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">-</span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">==</span>y<span class="token punctuation">)</span><span class="token comment">//是同一个节点</span>
		<span class="token keyword">return</span> x<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>lg<span class="token punctuation">[</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>i<span class="token operator">>=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">--</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">!=</span>gene<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//二分思想,直到跳到LCA的下面一层</span>
			x<span class="token operator">=</span>gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
			y<span class="token operator">=</span>gene<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">dist</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">)</span><span class="token comment">//x节点到y结点的距离</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> tem<span class="token operator">=</span><span class="token function">lca</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token function">abs</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">-</span>depth<span class="token punctuation">[</span>tem<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">abs</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">-</span>depth<span class="token punctuation">[</span>tem<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token keyword">int</span> s<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>nodes<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>nodes<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//	memset(gene,0,sizeof(gene));</span>
	depth<span class="token punctuation">[</span>s<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//预处理出log2(i)+1的值</span>
		lg<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>lg<span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span>lg<span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">==</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//不要写错</span>
	<span class="token function">dfs</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="应用"><a href="#应用" class="headerlink" title="应用"></a>应用</h5><ul>
<li><p>次小生成树：<a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P4180">P4180严格次小生成树</a></p>
</li>
<li><p>树上两条路径是否相交：<a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P3398">仓鼠找sugar </a></p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">u<span class="token operator">=</span><span class="token function">lca</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">,</span>v<span class="token operator">=</span><span class="token function">lca</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">dist</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>c<span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">dist</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>d<span class="token punctuation">)</span><span class="token operator">==</span><span class="token function">dist</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span>d<span class="token punctuation">)</span><span class="token operator">||</span><span class="token function">dist</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>a<span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">dist</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token operator">==</span><span class="token function">dist</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
	cout<span class="token operator">&lt;&lt;</span><span class="token string">"Yes"</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></li>
<li><p>给定节点，求以它为LCA的节点有多少种组合：<a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P5002">找祖先 </a>，DFS回溯求出</p>
</li>
<li><p>分类讨论求树上到任意两点距离相等的点的个数（重点讨论中节点与LCA关系）：<a target="_blank" rel="noopener" href="https://www.luogu.org/problem/CF519E">A and B and Lecture Rooms </a></p>
</li>
<li><p>求两节点路径上最大/最小边的权值，若求最小即为求容量：<a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P1967">货车运输 </a>，DFS时倍增求出路径上min</p>
</li>
</ul>
<h3 id="树上差分"><a href="#树上差分" class="headerlink" title="树上差分"></a>树上差分</h3><p>用于求解一些<strong>树上路径</strong>问题。</p>
<p>常见问法：将一棵树上从u到v路径上的<strong>点/边</strong>的权值加上x，询问<strong>某点/边</strong>的权值。</p>
<p>$O(1)$修改，$O(n)$查询，复杂度决定要离线。</p>
<p>强行在线可用<strong>树链剖分</strong>$O(logn \times logn)$修改，$O(logn \times logn)$查询。</p>
<p>修改差分数组之前先用DFS倍增求出各节点LCA。</p>
<p>差分数组记为power[maxn]，直接修改即可，查询时调用DFS</p>
<h4 id="性质"><a href="#性质" class="headerlink" title="性质"></a>性质</h4><ul>
<li>树上任意两点之间有且只有一条路径</li>
<li>一个节点只有一个父节点</li>
<li>$x$节点到$y$结点的路线为：$x→lca(x,y)→y$</li>
</ul>
<h4 id="点的差分"><a href="#点的差分" class="headerlink" title="点的差分"></a>点的差分</h4><h5 id="操作"><a href="#操作" class="headerlink" title="操作"></a>操作</h5><p>每次修改使$u$到$v$的路径上所有节点权值+1（包括端点），询问某一节点权值。</p>
<h5 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h5><p>$power[u]++,\ power[v]++,\ power[lca(u,v)]–,\ power[father[lca(u,v)]]–$</p>
<h4 id="边的差分"><a href="#边的差分" class="headerlink" title="边的差分"></a>边的差分</h4><h5 id="操作-1"><a href="#操作-1" class="headerlink" title="操作"></a>操作</h5><p><strong>以点代边</strong>，$power[x]$代表$x$节点到父节点的边的权值。</p>
<p>若查询边的权值，则需要按输入顺序对每条边进行编号。</p>
<p>每次修改使节点$u$与节点$v$之间<strong>所有边</strong>权值+1，询问某一条边权值。</p>
<h5 id="修改-1"><a href="#修改-1" class="headerlink" title="修改"></a>修改</h5><p>$power[u]++,\ power[v]++,\ power[lca(u,v)]-=2$</p>
<h4 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h4><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> power<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//power[x]即为x节点权值</span>
<span class="token keyword">void</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token comment">//查询，求出所有节点权值</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span><span class="token comment">//枚举x所有子节点</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">!=</span>gene<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//不为x父节点</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">dfs</span><span class="token punctuation">(</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//</span>
			power<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+=</span>power<span class="token punctuation">[</span>G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	ans<span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span>power<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="树链剖分"><a href="#树链剖分" class="headerlink" title="树链剖分"></a>树链剖分</h3><p>注意以点代边的思想</p>
<ul>
<li>功能<ol>
<li>更新/查询某个节点子树的权值</li>
<li>更新/查询树上两个节点间所有点的权值</li>
</ol>
</li>
<li>性质<ol>
<li>子树的时间戳一定全部小于父节点，并且连续（所以可用线段树维护）</li>
<li>任何一条路径都是由重链的一部分与重链间的叶子节点构成</li>
<li>任何父节点都一定在一条重链上（所以可用top的父节点跳链）</li>
</ol>
</li>
<li>例题<ol>
<li><a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P3384">P3384 树链剖分 </a></li>
</ol>
</li>
</ul>
<h4 id="通用模板"><a href="#通用模板" class="headerlink" title="通用模板"></a>通用模板</h4><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span>tot<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>head<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
ll val<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//给定的每个节点的权值</span>
<span class="token keyword">struct</span> <span class="token class-name">edge</span>
<span class="token punctuation">&#123;</span><span class="token comment">//边权一般不必记录到这里</span>
	<span class="token keyword">int</span> v<span class="token punctuation">,</span>w<span class="token punctuation">,</span>nex<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> e<span class="token punctuation">[</span>maxn<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> v<span class="token punctuation">,</span><span class="token keyword">int</span> w<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	e<span class="token punctuation">[</span><span class="token operator">++</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">=</span>v<span class="token punctuation">;</span>
	e<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token operator">=</span>w<span class="token punctuation">;</span>
	e<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token operator">=</span>head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span>tot<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//以点代边:以节点的权值代表该节点到父节点边的权值,修改与查询跳过链顶点即可(最终的参数改为dfn[x]+1)</span>
<span class="token comment">//使用前先初始化,然后加边,dfs1(rt,rt),dfs2(rt,rt),build(1,1,n),使用封装好的函数修改+查询</span>
<span class="token keyword">namespace</span> hld<span class="token punctuation">&#123;</span><span class="token comment">//heavy-light decomposition</span>
	<span class="token keyword">int</span> father<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>son<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>depth<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>siz<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//父节点,重儿子节点,深度,子树大小</span>
	<span class="token keyword">int</span> tim<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>rk<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>top<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//计数器,时间戳(节点编号),访问顺序,节点所在重链的顶部节点</span>
	ll w<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//节点dfs序对应权值</span>
	<span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		tim<span class="token operator">=</span>tot<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>head<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>head<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>depth<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>depth<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//		memset(father,0,sizeof(father));</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>son<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>son<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//		memset(top,0,sizeof(top));</span>
<span class="token comment">//		memset(dfn,0,sizeof(dfn));</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">dfs1</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> fa<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//预处理出深度,父节点,重儿子,子树大小</span>
		depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>depth<span class="token punctuation">[</span>fa<span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
		father<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>fa<span class="token punctuation">;</span>
		siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> maxsiz<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//遍历儿子节点</span>
			<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">==</span>fa<span class="token punctuation">)</span>
				<span class="token keyword">continue</span><span class="token punctuation">;</span>
<span class="token comment">//			val[v]=e[i].w;//以点代边:将边的权值赋给边头节点</span>
			<span class="token function">dfs1</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
			siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+=</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//加上儿子的子树大小</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>maxsiz<span class="token operator">&lt;</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>v<span class="token punctuation">;</span>
				maxsiz<span class="token operator">=</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//记录重儿子</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">dfs2</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> t<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//按dfs序对各节点重新编号,并记录对应权值到w数组</span>
		dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>tim<span class="token punctuation">;</span><span class="token comment">//记录dfs序</span>
		rk<span class="token punctuation">[</span>tim<span class="token punctuation">]</span><span class="token operator">=</span>x<span class="token punctuation">;</span><span class="token comment">//记录访问节点的顺序,即dfn的反函数</span>
		top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>t<span class="token punctuation">;</span><span class="token comment">//注意这里,top是在树外的</span>
		w<span class="token punctuation">[</span>tim<span class="token punctuation">]</span><span class="token operator">=</span>val<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//将x结点权值存到对应的时间戳</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//没有儿子</span>
			<span class="token keyword">return</span><span class="token punctuation">;</span>
		<span class="token function">dfs2</span><span class="token punctuation">(</span>son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//继续处理重儿子</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span><span class="token comment">//处理其他儿子</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">!=</span>father<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">!=</span>son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
				<span class="token function">dfs2</span><span class="token punctuation">(</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//开始另一条重链</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">lca</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">!=</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">&lt;</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
				<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
			x<span class="token operator">=</span>father<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> <span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">?</span>y<span class="token operator">:</span>x<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">struct</span> <span class="token class-name">node</span><span class="token comment">//线段树按dfs序维护树上路径权值部分</span>
	<span class="token punctuation">&#123;</span>
		ll val<span class="token punctuation">,</span>Max<span class="token punctuation">,</span>lazy<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> tree<span class="token punctuation">[</span>maxn<span class="token operator">&lt;&lt;</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushup</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">+</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token punctuation">,</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> l<span class="token punctuation">,</span><span class="token keyword">int</span> r<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>l<span class="token operator">==</span>r<span class="token punctuation">)</span><span class="token comment">//注意这里是l</span>
			tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token operator">=</span>w<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//按时间戳顺序的数组</span>
		<span class="token keyword">else</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>l<span class="token operator">+</span>r<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token function">build</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">,</span>l<span class="token punctuation">,</span>mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">build</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">,</span>mid<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">pushup</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushdown</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> l<span class="token punctuation">,</span><span class="token keyword">int</span> r<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>l<span class="token operator">+</span>r<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">+</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">*</span><span class="token punctuation">(</span>mid<span class="token operator">-</span>l<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">+</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">*</span><span class="token punctuation">(</span>r<span class="token operator">-</span>mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token operator">+=</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token punctuation">;</span><span class="token comment">//子节点最大值也要+更新值</span>
			tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token operator">+=</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">+=</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">+=</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">modify</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> nst<span class="token punctuation">,</span><span class="token keyword">int</span> ned<span class="token punctuation">,</span><span class="token keyword">int</span> ust<span class="token punctuation">,</span><span class="token keyword">int</span> ued<span class="token punctuation">,</span>ll num<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//区间更新</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>ned<span class="token operator">&lt;</span>ust<span class="token operator">||</span>ued<span class="token operator">&lt;</span>nst<span class="token punctuation">)</span>
			<span class="token keyword">return</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>ust<span class="token operator">&lt;=</span>nst<span class="token operator">&amp;&amp;</span>ued<span class="token operator">>=</span>ned<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">+</span><span class="token punctuation">(</span>ned<span class="token operator">-</span>nst<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span>num<span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token operator">+=</span>num<span class="token punctuation">;</span>
			tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">+=</span>num<span class="token punctuation">;</span>
			<span class="token keyword">return</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token function">pushdown</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span>nst<span class="token punctuation">,</span>ned<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>nst<span class="token operator">+</span>ned<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">modify</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">,</span>nst<span class="token punctuation">,</span>mid<span class="token punctuation">,</span>ust<span class="token punctuation">,</span>ued<span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">modify</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">,</span>mid<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>ned<span class="token punctuation">,</span>ust<span class="token punctuation">,</span>ued<span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">pushup</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	ll <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> nst<span class="token punctuation">,</span><span class="token keyword">int</span> ned<span class="token punctuation">,</span><span class="token keyword">int</span> qst<span class="token punctuation">,</span><span class="token keyword">int</span> qed<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//查询区间和</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>ned<span class="token operator">&lt;</span>qst<span class="token operator">||</span>qed<span class="token operator">&lt;</span>nst<span class="token punctuation">)</span>
			<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>qst<span class="token operator">&lt;=</span>nst<span class="token operator">&amp;&amp;</span>ned<span class="token operator">&lt;=</span>qed<span class="token punctuation">)</span>
			<span class="token keyword">return</span> tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">;</span>
		<span class="token function">pushdown</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span>nst<span class="token punctuation">,</span>ned<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>nst<span class="token operator">+</span>ned<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token function">query</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">,</span>nst<span class="token punctuation">,</span>mid<span class="token punctuation">,</span>qst<span class="token punctuation">,</span>qed<span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">query</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">,</span>mid<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>ned<span class="token punctuation">,</span>qst<span class="token punctuation">,</span>qed<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	ll <span class="token function">qmax</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> nst<span class="token punctuation">,</span><span class="token keyword">int</span> ned<span class="token punctuation">,</span><span class="token keyword">int</span> qst<span class="token punctuation">,</span><span class="token keyword">int</span> qed<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//查询区间和</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>ned<span class="token operator">&lt;</span>qst<span class="token operator">||</span>qed<span class="token operator">&lt;</span>nst<span class="token punctuation">)</span>
			<span class="token keyword">return</span> LLONG_MIN<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>qst<span class="token operator">&lt;=</span>nst<span class="token operator">&amp;&amp;</span>ned<span class="token operator">&lt;=</span>qed<span class="token punctuation">)</span>
			<span class="token keyword">return</span> tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>Max<span class="token punctuation">;</span>
		<span class="token function">pushdown</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span>nst<span class="token punctuation">,</span>ned<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>nst<span class="token operator">+</span>ned<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token function">max</span><span class="token punctuation">(</span><span class="token function">qmax</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">,</span>nst<span class="token punctuation">,</span>mid<span class="token punctuation">,</span>qst<span class="token punctuation">,</span>qed<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">qmax</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">,</span>mid<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>ned<span class="token punctuation">,</span>qst<span class="token punctuation">,</span>qed<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">mson</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span>ll addnum<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//将以x为根的子树全部加上一个数</span>
		<span class="token function">modify</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>addnum<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//子树节点编号是连续的</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">inline</span> ll <span class="token function">sonsum</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//查询以x为根子树权值和</span>
		<span class="token keyword">return</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//同上</span>
	<span class="token punctuation">&#125;</span>
	ll <span class="token function">sonmax</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">return</span> <span class="token function">qmax</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">mchain</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span>ll addnum<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">!=</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//不在同一条链上时</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">&lt;</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
				<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//保证x所在链顶部更低</span>
			<span class="token function">modify</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>addnum<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//更新顶部节点较低的重链(顶部节点到当前点部分)</span>
			x<span class="token operator">=</span>father<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//跳到链顶节点的父节点</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//直到最后在同一条重链上</span>
			<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//此时保证x节点在y上面</span>
		<span class="token function">modify</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span>addnum<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	ll <span class="token function">chainsum</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		ll ret<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">!=</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">&lt;</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
				<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
			ret<span class="token operator">+=</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			x<span class="token operator">=</span>father<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> ret<span class="token operator">+</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	ll <span class="token function">chainmax</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		ll ret<span class="token operator">=</span>LLONG_MIN<span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">!=</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">&lt;</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
				<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
			ret<span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span><span class="token function">qmax</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			x<span class="token operator">=</span>father<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token function">max</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span><span class="token function">qmax</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="P3384-树链剖分"><a href="#P3384-树链剖分" class="headerlink" title="P3384 树链剖分 "></a><a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P3384">P3384 树链剖分 </a></h4><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> inf<span class="token operator">=</span><span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">100005</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> mod<span class="token operator">=</span><span class="token number">100000007</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> head<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">edge</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> v<span class="token punctuation">,</span>nex<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> e<span class="token punctuation">[</span>maxn<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> father<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>son<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//父节点,重儿子节点</span>
<span class="token keyword">int</span> depth<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>siz<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>top<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//深度,子树大小,节点所在重链的顶部节点</span>
<span class="token keyword">int</span> tim<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>rk<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>w<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//计数器,时间戳(节点编号),访问顺序</span>
<span class="token keyword">int</span> val<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//给定的每个节点的权值</span>
<span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> v<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	e<span class="token punctuation">[</span><span class="token operator">++</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">=</span>v<span class="token punctuation">;</span>
	e<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token operator">=</span>head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span>cnt<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">dfs1</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> fa<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	father<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>fa<span class="token punctuation">;</span>
	depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>depth<span class="token punctuation">[</span>fa<span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
	siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> maxsize<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span><span class="token comment">//遍历儿子节点</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">==</span>fa<span class="token punctuation">)</span>
			<span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token function">dfs1</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
		siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+=</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//加上儿子的子树大小</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">></span>maxsize<span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			maxsize<span class="token operator">=</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span>
			son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>v<span class="token punctuation">;</span><span class="token comment">//记录重儿子</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">dfs2</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> t<span class="token punctuation">)</span><span class="token comment">//当前节点与重链顶节点</span>
<span class="token punctuation">&#123;</span>
	top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>t<span class="token punctuation">;</span><span class="token comment">//记录该节点所在重链的顶部节点</span>
	dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>tim<span class="token punctuation">;</span><span class="token comment">//记录该节点的访问时间(给节点编号,方便线段树操作)</span>
	rk<span class="token punctuation">[</span>tim<span class="token punctuation">]</span><span class="token operator">=</span>x<span class="token punctuation">;</span><span class="token comment">//记录访问节点的顺序</span>
	w<span class="token punctuation">[</span>tim<span class="token punctuation">]</span><span class="token operator">=</span>val<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//将x结点权值存到对应的时间戳</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token keyword">return</span><span class="token punctuation">;</span><span class="token comment">//没有儿子</span>
	<span class="token function">dfs2</span><span class="token punctuation">(</span>son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//继续处理重儿子</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//处理其他儿子</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">!=</span>son<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">!=</span>father<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token function">dfs2</span><span class="token punctuation">(</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//开始另一条重链</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">struct</span> <span class="token class-name">node</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> val<span class="token punctuation">,</span>lazy<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> tree<span class="token punctuation">[</span>maxn<span class="token operator">&lt;&lt;</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushup</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">+</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">build</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> l<span class="token punctuation">,</span><span class="token keyword">int</span> r<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>l<span class="token operator">==</span>r<span class="token punctuation">)</span><span class="token comment">//注意这里是l</span>
		tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span>w<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token operator">%</span>mod<span class="token punctuation">;</span><span class="token comment">//按时间戳顺序的数组</span>
	<span class="token keyword">else</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>l<span class="token operator">+</span>r<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token function">build</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">,</span>l<span class="token punctuation">,</span>mid<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">build</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">,</span>mid<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">pushup</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">pushdown</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> l<span class="token punctuation">,</span><span class="token keyword">int</span> r<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>l<span class="token operator">+</span>r<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">%</span>mod<span class="token operator">+</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">%</span>mod<span class="token operator">*</span><span class="token punctuation">(</span>mid<span class="token operator">-</span>l<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">%</span>mod<span class="token operator">+</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">%</span>mod<span class="token operator">*</span><span class="token punctuation">(</span>r<span class="token operator">-</span>mid<span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">=</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">%</span>mod<span class="token operator">+</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">%</span>mod<span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">=</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">%</span>mod<span class="token operator">+</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">%</span>mod<span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">modify</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> nst<span class="token punctuation">,</span><span class="token keyword">int</span> ned<span class="token punctuation">,</span><span class="token keyword">int</span> ust<span class="token punctuation">,</span><span class="token keyword">int</span> ued<span class="token punctuation">,</span><span class="token keyword">int</span> num<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>ned<span class="token operator">&lt;</span>ust<span class="token operator">||</span>ued<span class="token operator">&lt;</span>nst<span class="token punctuation">)</span>
		<span class="token keyword">return</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>ust<span class="token operator">&lt;=</span>nst<span class="token operator">&amp;&amp;</span>ued<span class="token operator">>=</span>ned<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">=</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>lazy<span class="token operator">%</span>mod<span class="token operator">+</span>num<span class="token operator">%</span>mod<span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
		tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">=</span><span class="token punctuation">(</span>tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">%</span>mod<span class="token operator">+</span><span class="token punctuation">(</span><span class="token punctuation">(</span>ned<span class="token operator">-</span>nst<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token operator">*</span><span class="token punctuation">(</span>num<span class="token operator">%</span>mod<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
		<span class="token keyword">return</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">pushdown</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span>nst<span class="token punctuation">,</span>ned<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>nst<span class="token operator">+</span>ned<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token function">modify</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">,</span>nst<span class="token punctuation">,</span>mid<span class="token punctuation">,</span>ust<span class="token punctuation">,</span>ued<span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">modify</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">,</span>mid<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>ned<span class="token punctuation">,</span>ust<span class="token punctuation">,</span>ued<span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">pushup</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> nst<span class="token punctuation">,</span><span class="token keyword">int</span> ned<span class="token punctuation">,</span><span class="token keyword">int</span> qst<span class="token punctuation">,</span><span class="token keyword">int</span> qed<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>ned<span class="token operator">&lt;</span>qst<span class="token operator">||</span>qed<span class="token operator">&lt;</span>nst<span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>qst<span class="token operator">&lt;=</span>nst<span class="token operator">&amp;&amp;</span>qed<span class="token operator">>=</span>ned<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">return</span> tree<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token operator">%</span>mod<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">pushdown</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span>nst<span class="token punctuation">,</span>ned<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>nst<span class="token operator">+</span>ned<span class="token punctuation">)</span><span class="token operator">>></span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token function">query</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">,</span>nst<span class="token punctuation">,</span>mid<span class="token punctuation">,</span>qst<span class="token punctuation">,</span>qed<span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">query</span><span class="token punctuation">(</span>root<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">,</span>mid<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>ned<span class="token punctuation">,</span>qst<span class="token punctuation">,</span>qed<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">mson</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> addnum<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//将以x为根的子树全部加上一个数</span>
	<span class="token function">modify</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>addnum<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//子树节点编号是连续的</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">inline</span> <span class="token keyword">int</span> <span class="token function">qson</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">return</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span><span class="token comment">//同上</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">mchain</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> addnum<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	addnum<span class="token operator">%=</span>mod<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">!=</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//不在同一条链上时</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">&lt;</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//保证x所在链顶部更低</span>
		<span class="token function">modify</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>addnum<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//更新顶部节点较低的重链(顶部节点到当前点部分)</span>
		x<span class="token operator">=</span>father<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//跳到链顶节点的父节点</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//直到最后在同一条重链上</span>
		<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//此时保证x节点在y上面</span>
	<span class="token function">modify</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span>addnum<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">qchain</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> ret<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">!=</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">&lt;</span>depth<span class="token punctuation">[</span>top<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
		ret<span class="token operator">=</span><span class="token punctuation">(</span>ret<span class="token operator">+</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
		x<span class="token operator">=</span>father<span class="token punctuation">[</span>top<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>depth<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">></span>depth<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token punctuation">(</span>ret<span class="token operator">+</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>dfn<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">%</span>mod<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
<span class="token comment">//	freopen("P3384.in","r",stdin);</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">,</span>p<span class="token punctuation">,</span>r<span class="token punctuation">;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">,</span><span class="token operator">&amp;</span>m<span class="token punctuation">,</span><span class="token operator">&amp;</span>r<span class="token punctuation">,</span><span class="token operator">&amp;</span>mod<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>head<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>head<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>val<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span>v<span class="token punctuation">;</span>
		<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>u<span class="token punctuation">,</span><span class="token operator">&amp;</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">add</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">add</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token function">dfs1</span><span class="token punctuation">(</span>r<span class="token punctuation">,</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">dfs2</span><span class="token punctuation">(</span>r<span class="token punctuation">,</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">build</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>m<span class="token operator">--</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> ope<span class="token punctuation">,</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>z<span class="token punctuation">;</span>
		<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>ope<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>ope<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//链x->y修改,全部加上z</span>
			<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>x<span class="token punctuation">,</span><span class="token operator">&amp;</span>y<span class="token punctuation">,</span><span class="token operator">&amp;</span>z<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">mchain</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>n<span class="token punctuation">,</span>z<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ope<span class="token operator">==</span><span class="token number">2</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//链x->y查询</span>
			<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>x<span class="token punctuation">,</span><span class="token operator">&amp;</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span><span class="token function">qchain</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ope<span class="token operator">==</span><span class="token number">3</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//x子树修改</span>
			<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>x<span class="token punctuation">,</span><span class="token operator">&amp;</span>z<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">mson</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>n<span class="token punctuation">,</span>z<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//x子树查询</span>
			<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span><span class="token function">qson</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token comment">//	for(int i=1;i&lt;=n;i++)</span>
<span class="token comment">//		printf("%d:%d\n",i,tree[i].val);</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">/*
 *功能：
 *1.更新/查询某个节点子树的权值
 *2.更新/查询树上两个节点间所有点的权值
 *性质：
 *1.子树的时间戳一定全部小于父节点，并且连续
 *2.任何一条路径都是由重链的一部分与重链间的叶子节点构成
 *3.任何父节点都一定在一条重链上
*/</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="点分治"><a href="#点分治" class="headerlink" title="点分治"></a>点分治</h3><h4 id="树的重心"><a href="#树的重心" class="headerlink" title="树的重心"></a>树的重心</h4><ul>
<li>也叫树的质心。找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后，生成的多棵树尽可能平衡。</li>
</ul>
<h4 id="流程-3"><a href="#流程-3" class="headerlink" title="流程"></a>流程</h4><ol>
<li>找出当前树的重心<ul>
<li>因为分治步骤二需要将sum赋值为当前树大小(siz[v])，所以getrt要跑两遍</li>
</ul>
</li>
<li>处理经过中心的路径<ul>
<li>点分治运算的核心，经常会出现变形</li>
</ul>
</li>
<li>删除树的重心</li>
<li>对新得到的子树重复上述步骤</li>
</ol>
<h4 id="例题"><a href="#例题" class="headerlink" title="例题"></a>例题</h4><p>一棵n节点的树，询问树上距离为k的点对是否存在。离线操作。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">10005</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxk<span class="token operator">=</span><span class="token number">10000005</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> tot<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>head<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">edge</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> v<span class="token punctuation">,</span>nex<span class="token punctuation">,</span>w<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> e<span class="token punctuation">[</span>maxn<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> v<span class="token punctuation">,</span>ll w<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	e<span class="token punctuation">[</span><span class="token operator">++</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">=</span>v<span class="token punctuation">;</span>
	e<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token operator">=</span>head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span>
	e<span class="token punctuation">[</span>tot<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token operator">=</span>w<span class="token punctuation">;</span>
	head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span>tot<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">,</span>root<span class="token punctuation">,</span>sum<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//重心,sum当前大小</span>
<span class="token keyword">int</span> siz<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>maxp<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">getrt</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> fa<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//DFS找重心</span>
	siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>maxp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//maxp为最大子树大小</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">==</span>fa<span class="token operator">||</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token function">getrt</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
		siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+=</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">></span>maxp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
			maxp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//记录下面的最大子树大小</span>
	<span class="token punctuation">&#125;</span><span class="token comment">//无根树,sum-siz[x]为以x的父节点为根的大小</span>
	<span class="token comment">//在以自身为根节点的子树大小和以x的父节点为根的大小中取较大的</span>
	maxp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>maxp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span>sum<span class="token operator">-</span>siz<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//sum为整棵树的大小</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>maxp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">&lt;</span>maxp<span class="token punctuation">[</span>root<span class="token punctuation">]</span><span class="token punctuation">)</span>
		root<span class="token operator">=</span>x<span class="token punctuation">;</span><span class="token comment">//最大子树最小的点为重心</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> dist<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>tmp<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//cnt计数器</span>
<span class="token keyword">void</span> <span class="token function">getdist</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> fa<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//DFS求各点到root的距离,记录在tmp中</span>
	tmp<span class="token punctuation">[</span><span class="token operator">++</span>cnt<span class="token punctuation">]</span><span class="token operator">=</span>dist<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>v<span class="token operator">==</span>fa<span class="token operator">||</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token keyword">continue</span><span class="token punctuation">;</span>
		dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>dist<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span>
		<span class="token function">getdist</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> q<span class="token punctuation">[</span><span class="token number">105</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//q记录询问距离</span>
<span class="token keyword">bool</span> jud<span class="token punctuation">[</span>maxk<span class="token punctuation">]</span><span class="token punctuation">,</span>ans<span class="token punctuation">[</span><span class="token number">105</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//存放之前子树中的存在路径长度,ans判断k是否存在</span>
queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> QwQ<span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">solve</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//处理经过根节点x的路径</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//该点已经被去掉</span>
			<span class="token keyword">continue</span><span class="token punctuation">;</span>
		cnt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span><span class="token comment">//设置root与儿子的距离</span>
		<span class="token function">getdist</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>cnt<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//遍历该子树上的距离</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>k<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>k<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//遍历询问</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>q<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token operator">>=</span>tmp<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//有拼出来的可能性</span>
					ans<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token operator">|=</span>jud<span class="token punctuation">[</span>q<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token operator">-</span>tmp<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//可以用之前以x为顶的距离拼起来</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>cnt<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//将这棵子树的距离存起来</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//供之后的以x为节点的子树拼路径使用</span>
			QwQ<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tmp<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			jud<span class="token punctuation">[</span>tmp<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>QwQ<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		jud<span class="token punctuation">[</span>QwQ<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		QwQ<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">divide</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	vis<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>jud<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//去掉根节点x</span>
	<span class="token function">solve</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//处理所有经过x的路径</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>head<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token operator">~</span>i<span class="token punctuation">;</span>i<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nex<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> v<span class="token operator">=</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token keyword">continue</span><span class="token punctuation">;</span>
		maxp<span class="token punctuation">[</span>root<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span>sum<span class="token operator">=</span>siz<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//重心置为0,maxp[0]置为最大值(所以要重新DFS计算siz)</span>
		<span class="token function">getrt</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在以v为根的子树上找重心</span>
		<span class="token function">getrt</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//处理出以v为根的siz数组</span>
		<span class="token function">divide</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> k<span class="token punctuation">,</span>u<span class="token punctuation">,</span>v<span class="token punctuation">;</span>
	ll w<span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>head<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>head<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	cin<span class="token operator">>></span>n<span class="token operator">>></span>m<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//点u到点v距离为w</span>
		cin<span class="token operator">>></span>u<span class="token operator">>></span>v<span class="token operator">>></span>w<span class="token punctuation">;</span>
		<span class="token function">add</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>v<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">add</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span>u<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		cin<span class="token operator">>></span>q<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	maxp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span>sum<span class="token operator">=</span>n<span class="token punctuation">;</span><span class="token comment">//置为最大值</span>
	<span class="token function">getrt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">getrt</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//更新以重心为根的siz数组</span>
	<span class="token function">divide</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		cout<span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span>ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">?</span><span class="token string">"AYE"</span><span class="token operator">:</span><span class="token string">"NAY"</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span><span class="token string">'\n'</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="树上启发式合并（DSU-on-tree）"><a href="#树上启发式合并（DSU-on-tree）" class="headerlink" title="树上启发式合并（DSU on tree）"></a>树上启发式合并（DSU on tree）</h3><p>解决<strong>离线子树查询</strong>问题，即统计树上一个节点的子树中具有某种特征的节点数。</p>
<p>一般也可以使用DFS序莫队或DFS序主席树做。</p>
<p>时间复杂度$O(nlogn)$，空间复杂度$O(n)$。</p>
<h4 id="流程-4"><a href="#流程-4" class="headerlink" title="流程"></a>流程</h4><ol>
<li>先用dfs处理出重儿子</li>
<li>使用DFS处理各子树信息，设当前子树根节点为x<ul>
<li><strong>遍历</strong>x的轻儿子，计算轻儿子子树贡献，<strong>记录到ans数组</strong>，信息不做保留。</li>
<li>处理x的重儿子子树贡献，<strong>记录到ans数组</strong>，并保留。</li>
<li>暴力统计节点x及所有轻儿子子树贡献，与x的重儿子子树贡献汇总，一同<strong>回溯</strong>到上一级，以便处理出以x节点的父节点为根的子树的贡献。</li>
</ul>
</li>
</ol>
<h3 id="二分图"><a href="#二分图" class="headerlink" title="二分图"></a>二分图</h3><p><del>二分图如果不考虑复杂度的话，可以用网络流来做。</del></p>
<h4 id="定义-1"><a href="#定义-1" class="headerlink" title="定义"></a>定义</h4><ul>
<li>最大匹配：二分图中边集的数目最大的匹配。</li>
<li>点覆盖：图$G=(V,E)$中的一个点覆盖为一个集合$S⊆V$使得<strong>每条边至少有一个端点</strong>在$S$中。</li>
<li>最小点覆盖：点个数最少的$S$集合。</li>
<li>最小边覆盖：用最少不相交简单路径覆盖DAG所有顶点。</li>
<li>最小点权覆盖：覆盖每个节点都需要一定代价，覆盖所有边总代价最小的点集。</li>
<li>最大独立集：在点集$V$中选出$M$个点，$M$中点与点两两无边，并使$M$最大。</li>
</ul>
<h4 id="常见二分图结论"><a href="#常见二分图结论" class="headerlink" title="常见二分图结论"></a>常见二分图结论</h4><ul>
<li><strong>最小点覆盖=二分图最大匹配</strong></li>
<li>最小边覆盖=顶点数-最小顶点覆盖（二分图最大匹配）</li>
<li>最大独立集=顶点数-最大匹配数</li>
<li>所有回路长度均为偶数</li>
</ul>
<h4 id="适用任意图的结论"><a href="#适用任意图的结论" class="headerlink" title="适用任意图的结论"></a>适用任意图的结论</h4><ul>
<li><p>对于不存在孤立点的图，最大匹配+最小边覆盖=顶点数</p>
</li>
<li><p>最大独立集+最小顶点覆盖=顶点数</p>
</li>
</ul>
<h4 id="二分图判定（黑白染色法）"><a href="#二分图判定（黑白染色法）" class="headerlink" title="二分图判定（黑白染色法）"></a>二分图判定（黑白染色法）</h4><p>A、B集合分别染成不同颜色（由边链接的两节点颜色一定不同）</p>
<p>A集合中选取一个起始节点，将邻接的点染成与其不同的颜色，如果邻接的点有相同颜色的，则说明不是二分图</p>
<h4 id="匈牙利算法"><a href="#匈牙利算法" class="headerlink" title="匈牙利算法"></a>匈牙利算法</h4><h5 id="邻接表"><a href="#邻接表" class="headerlink" title="邻接表"></a>邻接表</h5><p>时间复杂度O(nm)，有重边时使用邻接矩阵？</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">205</span><span class="token punctuation">;</span><span class="token comment">//在主函数内向关系图G中加边</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//注意使用前clear()</span>
<span class="token keyword">int</span> linker<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> used<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token comment">//</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> <span class="token operator">&amp;</span>v<span class="token operator">:</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>used<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//在右边找</span>
		<span class="token punctuation">&#123;</span>
			used<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>linker<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">||</span><span class="token function">dfs</span><span class="token punctuation">(</span>linker<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				linker<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token operator">=</span>x<span class="token punctuation">;</span><span class="token comment">//记录右边v号匹配</span>
				<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//未找到增广路</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">hungry</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>linker<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>linker<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//遍历左面</span>
	<span class="token punctuation">&#123;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>used<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>used<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">dfs</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//能找到增广路</span>
			ans<span class="token operator">++</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="KM算法"><a href="#KM算法" class="headerlink" title="KM算法"></a>KM算法</h4><ul>
<li>二分图最佳完美匹配：带权二分图，求一种<strong>完备匹配</strong>方案，使得所有<strong>匹配边的权和最大</strong>。</li>
</ul>
<h5 id="BFS，复杂度稳的一批"><a href="#BFS，复杂度稳的一批" class="headerlink" title="BFS，复杂度稳的一批"></a>BFS，复杂度稳的一批</h5><p>复杂度$O(n^3)$，使用时清空w数组，并且按照关系为w赋值，调用KM(n)即可得到匹配边权值和。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">1005</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> w<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//二分图间的权值</span>
<span class="token keyword">int</span> lx<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>ly<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> linker<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//B图匹配到的A图节点</span>
<span class="token keyword">int</span> slack<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> visy<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//记录每一轮B图匹配过</span>
<span class="token keyword">int</span> pre<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">bfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
    <span class="token keyword">int</span> x<span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>yy<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>delta<span class="token punctuation">;</span>
    <span class="token function">memset</span><span class="token punctuation">(</span>pre<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>pre<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>INF<span class="token punctuation">;</span>
    linker<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">=</span>k<span class="token punctuation">;</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
        x<span class="token operator">=</span>linker<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">;</span>
        delta<span class="token operator">=</span>INF<span class="token punctuation">;</span>
        visy<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>visy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">&#123;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">></span>lx<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>ly<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-</span>w<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token punctuation">&#123;</span>
                    slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>lx<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span>ly<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-</span>w<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
                    pre<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>y<span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">&lt;</span>delta<span class="token punctuation">)</span>
					delta<span class="token operator">=</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>yy<span class="token operator">=</span>i<span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span> visy<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token punctuation">)</span>
				lx<span class="token punctuation">[</span>linker<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">-=</span>delta<span class="token punctuation">,</span>ly<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">+=</span>delta<span class="token punctuation">;</span>
            <span class="token keyword">else</span>
				slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-=</span>delta<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        y<span class="token operator">=</span>yy<span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>linker<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">==</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
			<span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span>
		linker<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token operator">=</span>linker<span class="token punctuation">[</span>pre<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>y<span class="token operator">=</span>pre<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">KM</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
    <span class="token function">memset</span><span class="token punctuation">(</span>lx<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>lx<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">memset</span><span class="token punctuation">(</span>ly<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>ly<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">memset</span><span class="token punctuation">(</span>linker<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>linker<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
        <span class="token function">memset</span><span class="token punctuation">(</span>visy<span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>visy<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">bfs</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>linker<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">!=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
			ans<span class="token operator">+=</span>w<span class="token punctuation">[</span>linker<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="左右数目不等的模板"><a href="#左右数目不等的模板" class="headerlink" title="左右数目不等的模板"></a>左右数目不等的模板</h5><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> wx<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>wy<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>match<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> mp<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>slack<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span>pre<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> viy<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">bfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> m<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> py<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>px<span class="token punctuation">,</span>yy<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>delta<span class="token punctuation">;</span>
	match<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token operator">=</span>k<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>pre<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>inf<span class="token punctuation">;</span>
	<span class="token keyword">do</span>
    <span class="token punctuation">&#123;</span>
		px<span class="token operator">=</span>match<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token punctuation">,</span>delta<span class="token operator">=</span>inf<span class="token punctuation">,</span>viy<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>viy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">&#123;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>wx<span class="token punctuation">[</span>px<span class="token punctuation">]</span><span class="token operator">+</span>wy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-</span>mp<span class="token punctuation">[</span>px<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">&lt;</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>wx<span class="token punctuation">[</span>px<span class="token punctuation">]</span><span class="token operator">+</span>wy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-</span>mp<span class="token punctuation">[</span>px<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>pre<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>py<span class="token punctuation">;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">&lt;</span>delta<span class="token punctuation">)</span>delta<span class="token operator">=</span>slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>yy<span class="token operator">=</span>i<span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>viy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>wx<span class="token punctuation">[</span>match<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">-=</span>delta<span class="token punctuation">,</span>wy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">+=</span>delta<span class="token punctuation">;</span>
			<span class="token keyword">else</span> slack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">-=</span>delta<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
		py<span class="token operator">=</span>yy<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span><span class="token keyword">while</span><span class="token punctuation">(</span>match<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token operator">!=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>py<span class="token punctuation">)</span>match<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token operator">=</span>match<span class="token punctuation">[</span>pre<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>py<span class="token operator">=</span>pre<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">km</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> m<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//n>=m,mp[m][n]这样输入匹配权值</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
		wy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>match<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">&#123;</span>
		wx<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>
			wx<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>wx<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>mp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span><span class="token operator">++</span>i<span class="token punctuation">)</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>viy<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>viy<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">bfs</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span>n<span class="token punctuation">,</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ans<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span><span class="token operator">++</span>i<span class="token punctuation">)</span>
		ans<span class="token operator">+=</span>wx<span class="token punctuation">[</span>match<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">+</span>wy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="CSL的写法"><a href="#CSL的写法" class="headerlink" title="CSL的写法"></a>CSL的写法</h5>  <pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">const</span> <span class="token keyword">int</span> maxn <span class="token operator">=</span> <span class="token number">305</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> INF<span class="token operator">=</span><span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span><span class="token comment">//KM算法:带权的二分图中寻找*权值和最大*的完备匹配</span>
<span class="token keyword">int</span> cost<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//A[i]连接B[j]的权值</span>
<span class="token keyword">int</span> lx<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span> ly<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> match<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span> slack<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//B[i]匹配到的A,</span>
<span class="token keyword">int</span> previous<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> vy<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//匹配过</span>
<span class="token keyword">void</span> <span class="token function">augment</span><span class="token punctuation">(</span><span class="token keyword">int</span> root<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token function">fill</span><span class="token punctuation">(</span>vy <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> vy <span class="token operator">+</span> n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">fill</span><span class="token punctuation">(</span>slack <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> slack <span class="token operator">+</span> n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> INF<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> py<span class="token punctuation">;</span>
	match<span class="token punctuation">[</span>py <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> root<span class="token punctuation">;</span>
	<span class="token keyword">do</span>
	<span class="token punctuation">&#123;</span>
		vy<span class="token punctuation">[</span>py<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> x <span class="token operator">=</span> match<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token punctuation">,</span> yy<span class="token punctuation">;</span>
		<span class="token keyword">int</span> delta <span class="token operator">=</span> INF<span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> y <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> y <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> y<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>vy<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				<span class="token keyword">if</span> <span class="token punctuation">(</span>lx<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> ly<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">-</span> cost<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">&lt;</span> slack<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
					slack<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> lx<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> ly<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">-</span> cost<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> previous<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> py<span class="token punctuation">;</span>
				<span class="token keyword">if</span> <span class="token punctuation">(</span>slack<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">&lt;</span> delta<span class="token punctuation">)</span>
					delta <span class="token operator">=</span> slack<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">,</span> yy <span class="token operator">=</span> y<span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> y <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> y <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> y<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>vy<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">)</span>
				lx<span class="token punctuation">[</span>match<span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">-=</span> delta<span class="token punctuation">,</span> ly<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">+=</span> delta<span class="token punctuation">;</span>
			<span class="token keyword">else</span>
				slack<span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">-=</span> delta<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		py <span class="token operator">=</span> yy<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>match<span class="token punctuation">[</span>py<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">do</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> pre <span class="token operator">=</span> previous<span class="token punctuation">[</span>py<span class="token punctuation">]</span><span class="token punctuation">;</span>
		match<span class="token punctuation">[</span>py<span class="token punctuation">]</span> <span class="token operator">=</span> match<span class="token punctuation">[</span>pre<span class="token punctuation">]</span><span class="token punctuation">,</span> py <span class="token operator">=</span> pre<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>py<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">KM</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		lx<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> ly<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		match<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span>
			lx<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>lx<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> cost<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> answer <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> root <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> root <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> root<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token function">augment</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
		answer <span class="token operator">+=</span> lx<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> answer <span class="token operator">+=</span> ly<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> answer<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>




<h3 id="网络流"><a href="#网络流" class="headerlink" title="网络流"></a>网络流</h3><p>注意反向思考，添加新节点进行限流。</p>
<h4 id="模板部分"><a href="#模板部分" class="headerlink" title="模板部分"></a>模板部分</h4><h5 id="dinic"><a href="#dinic" class="headerlink" title="dinic"></a>dinic</h5><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">struct</span> <span class="token class-name">Dinic</span>
<span class="token punctuation">&#123;</span><span class="token comment">//复杂度O(n^2m)</span>
	<span class="token keyword">struct</span> <span class="token class-name">Edge</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> from<span class="token punctuation">,</span> to<span class="token punctuation">,</span> cap<span class="token punctuation">,</span> flow<span class="token punctuation">;</span>
		<span class="token function">Edge</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">,</span> <span class="token keyword">int</span> c<span class="token punctuation">,</span> <span class="token keyword">int</span> f<span class="token punctuation">)</span><span class="token operator">:</span>
			<span class="token function">from</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">to</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">flow</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> s<span class="token punctuation">,</span> t<span class="token punctuation">;</span> <span class="token comment">//结点数,边数(包括反向弧),源点编号和汇点编号</span>
	vector<span class="token operator">&lt;</span>Edge<span class="token operator">></span> edges<span class="token punctuation">;</span> <span class="token comment">//边表。edge[e]和edge[e^1]互为反向弧</span>
	vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//邻接表，G[i][j]表示节点i的第j条边在e数组中的序号</span>
	<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//BFS使用</span>
	<span class="token keyword">int</span> d<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//从起点到i的距离</span>
	<span class="token keyword">int</span> cur<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//当前弧下标</span>
	<span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>n <span class="token operator">=</span> n<span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		edges<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">AddEdge</span><span class="token punctuation">(</span><span class="token keyword">int</span> from<span class="token punctuation">,</span> <span class="token keyword">int</span> to<span class="token punctuation">,</span> <span class="token keyword">int</span> cap<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		edges<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Edge</span><span class="token punctuation">(</span>from<span class="token punctuation">,</span> to<span class="token punctuation">,</span> cap<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		edges<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Edge</span><span class="token punctuation">(</span>to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//反向弧,初始容量为0</span>
		m <span class="token operator">=</span> edges<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		G<span class="token punctuation">[</span>from<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>m <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		G<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">bool</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//		memset(d, 0, sizeof(d));</span>
		queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span>
		q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
		d<span class="token punctuation">[</span>s<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		vis<span class="token punctuation">[</span>s<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> x <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				Edge<span class="token operator">&amp;</span> e <span class="token operator">=</span> edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> e<span class="token punctuation">.</span>cap <span class="token operator">></span> e<span class="token punctuation">.</span>flow<span class="token punctuation">)</span>
				<span class="token punctuation">&#123;</span><span class="token comment">//只考虑残量网络中的弧</span>
					vis<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
					d<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span> <span class="token operator">=</span> d<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//构造分层图</span>
					q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>to<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> vis<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//有无增广路,s->t</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">DFS</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> a<span class="token punctuation">)</span><span class="token comment">//x为当前点,a为当前边上流量</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//在层次图上向t延伸,多路增广</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">==</span>t<span class="token operator">||</span>a<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">//到达目标/流量为0断流</span>
			<span class="token keyword">return</span> a<span class="token punctuation">;</span>
		<span class="token keyword">int</span> flow <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> f<span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token operator">&amp;</span> i<span class="token operator">=</span>cur<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//从上一次x遍历跑到的点开始跑</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//从上次考虑的弧</span>
			Edge<span class="token operator">&amp;</span> e <span class="token operator">=</span> edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>d<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">1</span><span class="token operator">==</span>d<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span><span class="token punctuation">(</span>f<span class="token operator">=</span><span class="token function">DFS</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>to<span class="token punctuation">,</span><span class="token function">min</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>e<span class="token punctuation">.</span>cap<span class="token operator">-</span>e<span class="token punctuation">.</span>flow<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span><span class="token comment">//只从层数编号较小的点到下一层的点</span>
				e<span class="token punctuation">.</span>flow <span class="token operator">+=</span> f<span class="token punctuation">;</span><span class="token comment">//该路径上边流量都增加f</span>
				edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">^</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>flow <span class="token operator">-=</span> f<span class="token punctuation">;</span><span class="token comment">//方便反悔</span>
				flow <span class="token operator">+=</span> f<span class="token punctuation">;</span>
				a <span class="token operator">-=</span> f<span class="token punctuation">;</span><span class="token comment">//用去可增广量</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>a<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">//a等于0及时退出</span>
					<span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment">//当a!=0,说明当前节点还存在另一个增广路分支。</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>flow<span class="token punctuation">)</span><span class="token comment">//增广后容量满了</span>
			d<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//炸点优化,不必要的点下一次就不用遍历</span>
		<span class="token keyword">return</span> flow<span class="token punctuation">;</span><span class="token comment">//返回x节点最大流量,传递到上一级</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">Maxflow</span><span class="token punctuation">(</span><span class="token keyword">int</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> t<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>s <span class="token operator">=</span> s<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token operator">-></span>t <span class="token operator">=</span> t<span class="token punctuation">;</span>
		<span class="token keyword">int</span> flow <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//不停地用bfs构造分层网络，然后用dfs沿着阻塞流增广</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">memset</span><span class="token punctuation">(</span>cur<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//建完分层图后cur也要初始化</span>
			flow <span class="token operator">+=</span> <span class="token function">DFS</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>inf<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> flow<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> di<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="dijkstra费用流"><a href="#dijkstra费用流" class="headerlink" title="dijkstra费用流"></a>dijkstra费用流</h4><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">struct</span> <span class="token class-name">MCMF</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">struct</span> <span class="token class-name">Edge</span> <span class="token punctuation">&#123;</span>
		ll v<span class="token punctuation">,</span> cap<span class="token punctuation">,</span> cost<span class="token punctuation">,</span> rev<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	<span class="token keyword">const</span> ll INF<span class="token operator">=</span><span class="token number">0x3f3f3f3f3f3f3f3f</span><span class="token punctuation">;</span>
	ll flow<span class="token punctuation">,</span> cost<span class="token punctuation">,</span> s<span class="token punctuation">,</span> t<span class="token punctuation">,</span> n<span class="token punctuation">;</span><span class="token comment">//前驱结点和对应边</span>
	ll dist<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span> H<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span> pv<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">,</span> pe<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//H为节点势函数</span>
	std<span class="token operator">::</span>vector<span class="token operator">&lt;</span>Edge<span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//因为要记录前驱,不能用前向星</span>
	<span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>n <span class="token operator">=</span> n<span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">void</span> <span class="token function">addEdge</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">,</span> <span class="token keyword">int</span> cap<span class="token punctuation">,</span> ll cost<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//dojk费用流中两节点间流向单向</span>
		G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>v<span class="token punctuation">,</span>cap<span class="token punctuation">,</span>cost<span class="token punctuation">,</span>G<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		G<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>u<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span>cost<span class="token punctuation">,</span>G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">bool</span> <span class="token function">dijkstra</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">//这里是单路增广</span>
	<span class="token punctuation">&#123;</span><span class="token comment">//复杂度相对SPFA稳定</span>
		std<span class="token operator">::</span>priority_queue<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span>ll<span class="token punctuation">,</span>ll<span class="token operator">></span><span class="token punctuation">,</span> std<span class="token operator">::</span>vector<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span>ll<span class="token punctuation">,</span>ll<span class="token operator">>></span><span class="token punctuation">,</span> std<span class="token operator">::</span>greater<span class="token operator">&lt;</span>pair<span class="token operator">&lt;</span>ll<span class="token punctuation">,</span>ll<span class="token operator">>></span> <span class="token operator">></span> q<span class="token punctuation">;</span>
		std<span class="token operator">::</span><span class="token function">fill</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> dist <span class="token operator">+</span> n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> INF<span class="token punctuation">)</span><span class="token punctuation">;</span>
		dist<span class="token punctuation">[</span>s<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span> <span class="token number">0</span><span class="token punctuation">,</span> s <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//到x距离即为到x的单位花费之和</span>
			pair<span class="token operator">&lt;</span>ll<span class="token punctuation">,</span>ll<span class="token operator">></span> x <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			ll<span class="token operator">&amp;</span> u <span class="token operator">=</span> x<span class="token punctuation">.</span>second<span class="token punctuation">;</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">&lt;</span> x<span class="token punctuation">.</span>first<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//不能优化距离</span>
			<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				Edge<span class="token operator">&amp;</span> e <span class="token operator">=</span> G<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//当前边</span>
				ll<span class="token operator">&amp;</span> v <span class="token operator">=</span> e<span class="token punctuation">.</span>v<span class="token punctuation">;</span>
				pair<span class="token operator">&lt;</span>ll<span class="token punctuation">,</span>ll<span class="token operator">></span> <span class="token function">y</span><span class="token punctuation">(</span>dist<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">+</span> e<span class="token punctuation">.</span>cost <span class="token operator">+</span> H<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">-</span> H<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">,</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span> <span class="token punctuation">(</span>e<span class="token punctuation">.</span>cap <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span> <span class="token operator">></span> y<span class="token punctuation">.</span>first<span class="token punctuation">)</span>
				<span class="token punctuation">&#123;</span>
					dist<span class="token punctuation">[</span>v<span class="token punctuation">]</span> <span class="token operator">=</span> y<span class="token punctuation">.</span>first<span class="token punctuation">;</span>
					pv<span class="token punctuation">[</span>v<span class="token punctuation">]</span> <span class="token operator">=</span> u<span class="token punctuation">,</span>pe<span class="token punctuation">[</span>v<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span><span class="token comment">//前驱点与前驱边</span>
					q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">==</span> INF<span class="token punctuation">)</span><span class="token comment">//无法增广</span>
			<span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token comment">//更新每轮的势函数</span>
			H<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+=</span> dist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		ll f <span class="token operator">=</span> INF<span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> v <span class="token operator">=</span> t<span class="token punctuation">;</span> v <span class="token operator">!=</span> s<span class="token punctuation">;</span> v <span class="token operator">=</span> pv<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//沿增广路回到起点</span>
			f <span class="token operator">=</span> std<span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span> G<span class="token punctuation">[</span>pv<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>pe<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cap<span class="token punctuation">)</span><span class="token punctuation">;</span>
		flow <span class="token operator">+=</span> f<span class="token punctuation">;</span><span class="token comment">//每次增广一条路径,这条路径增广量就是新增的流量</span>
		cost <span class="token operator">+=</span> f <span class="token operator">*</span> H<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//h[t]-h[s]即为s到t的路径长</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> v <span class="token operator">=</span> t<span class="token punctuation">;</span> v <span class="token operator">!=</span> s<span class="token punctuation">;</span> v <span class="token operator">=</span> pv<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//更新增广路容量信息</span>
			Edge<span class="token operator">&amp;</span> e <span class="token operator">=</span> G<span class="token punctuation">[</span>pv<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>pe<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			e<span class="token punctuation">.</span>cap <span class="token operator">-=</span> f<span class="token punctuation">;</span>
			G<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">[</span>e<span class="token punctuation">.</span>rev<span class="token punctuation">]</span><span class="token punctuation">.</span>cap <span class="token operator">+=</span> f<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	ll <span class="token function">solve</span><span class="token punctuation">(</span><span class="token keyword">int</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> t<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>s <span class="token operator">=</span> s<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token operator">-></span>t <span class="token operator">=</span> t<span class="token punctuation">;</span>
		flow <span class="token operator">=</span> cost <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		std<span class="token operator">::</span><span class="token function">fill</span><span class="token punctuation">(</span>H<span class="token punctuation">,</span> H <span class="token operator">+</span> n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//初始网络为0非负,势函数也为0</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token function">dijkstra</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//每次选择最小费用增广路径一定是当前残留图的最小增广路径</span>
		<span class="token keyword">return</span> flow<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> mcmf<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="最大流"><a href="#最大流" class="headerlink" title="最大流"></a>最大流</h4><h5 id="太空飞行计划问题（最小割-收益最大问题）"><a href="#太空飞行计划问题（最小割-收益最大问题）" class="headerlink" title="太空飞行计划问题（最小割,收益最大问题）"></a>太空飞行计划问题（最小割,收益最大问题）</h5><p> <strong>最优收益 = 所有实验的报酬总和 - 该图的最大流</strong></p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> m<span class="token punctuation">,</span>n<span class="token punctuation">;</span><span class="token comment">//实验数,仪器数</span>
<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>m<span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> s<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>t<span class="token operator">=</span>m<span class="token operator">+</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>sum<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
di<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> profit<span class="token punctuation">,</span>equ<span class="token punctuation">;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>profit<span class="token punctuation">)</span><span class="token punctuation">;</span>
	sum<span class="token operator">+=</span>profit<span class="token punctuation">;</span><span class="token comment">//sum为所有收益之和</span>
	di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>i<span class="token punctuation">,</span>profit<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//源点到实验</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">char</span> ch<span class="token punctuation">;</span>
		<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%c"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>equ<span class="token punctuation">,</span><span class="token operator">&amp;</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span>
		di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span>m<span class="token operator">+</span>equ<span class="token punctuation">,</span>inf<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//实验到器材</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token operator">==</span><span class="token string">'\r'</span><span class="token operator">||</span>ch<span class="token operator">==</span><span class="token string">'\n'</span><span class="token punctuation">)</span>
			<span class="token keyword">break</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> cost<span class="token punctuation">;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>cost<span class="token punctuation">)</span><span class="token punctuation">;</span>
	di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>m<span class="token operator">+</span>i<span class="token punctuation">,</span>t<span class="token punctuation">,</span>cost<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//器材到汇点</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> ans<span class="token operator">=</span>di<span class="token punctuation">.</span><span class="token function">Maxflow</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>di<span class="token punctuation">.</span>d<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//选择去做的实验编号</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d "</span><span class="token punctuation">,</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token function">putchar</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>di<span class="token punctuation">.</span>d<span class="token punctuation">[</span>m<span class="token operator">+</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//需要购买的器材编号</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d "</span><span class="token punctuation">,</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\n%d\n"</span><span class="token punctuation">,</span>sum<span class="token operator">-</span>ans<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>建图后跑最大流求出最小割，满流的实验边割掉，满流的设备边也在割集里(但是这是需要购买的)，该最小割即为最小亏损。<br>所以最后一次BFS，所有未被割掉的实验为非饱和弧，可以求出深度。<br>所以未被割掉的实验(及选择去做的实验)所连接的设备同样可以求出深度。</p>
<h5 id="最小路径覆盖问题-最大流-最小不相交路径覆盖模型-要求路径数最少-路径输出"><a href="#最小路径覆盖问题-最大流-最小不相交路径覆盖模型-要求路径数最少-路径输出" class="headerlink" title="最小路径覆盖问题(最大流,最小不相交路径覆盖模型,要求路径数最少,路径输出)"></a>最小路径覆盖问题(最大流,最小不相交路径覆盖模型,要求路径数最少,路径输出)</h5><p>二分图定理之一：最小路径覆盖数=顶点数-最大匹配</p>
<p>不要使用炸点优化！！！</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//#pragma G++ optimize("O2")</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> maxn <span class="token operator">=</span> <span class="token number">13005</span><span class="token punctuation">;</span><span class="token comment">//上大的模板</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> inf<span class="token operator">=</span><span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> nex<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> vist<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> all<span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">Dinic</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">struct</span> <span class="token class-name">Edge</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> from<span class="token punctuation">,</span> to<span class="token punctuation">,</span> cap<span class="token punctuation">,</span> flow<span class="token punctuation">;</span>
		<span class="token function">Edge</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">,</span> <span class="token keyword">int</span> c<span class="token punctuation">,</span> <span class="token keyword">int</span> f<span class="token punctuation">)</span><span class="token operator">:</span>
			<span class="token function">from</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">to</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">flow</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> s<span class="token punctuation">,</span> t<span class="token punctuation">;</span> <span class="token comment">//结点数,边数(包括反向弧),源点编号和汇点编号</span>
	vector<span class="token operator">&lt;</span>Edge<span class="token operator">></span> edges<span class="token punctuation">;</span> <span class="token comment">//边表。edge[e]和edge[e^1]互为反向弧</span>
	vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> G<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//邻接表，G[i][j]表示节点i的第j条边在e数组中的序号</span>
	<span class="token keyword">bool</span> vis<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//BFS使用</span>
	<span class="token keyword">int</span> d<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//从起点到i的距离</span>
	<span class="token keyword">int</span> cur<span class="token punctuation">[</span>maxn<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//当前弧下标</span>
	<span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>n <span class="token operator">=</span> n<span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
			G<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		edges<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">AddEdge</span><span class="token punctuation">(</span><span class="token keyword">int</span> from<span class="token punctuation">,</span> <span class="token keyword">int</span> to<span class="token punctuation">,</span> <span class="token keyword">int</span> cap<span class="token punctuation">)</span>
    <span class="token punctuation">&#123;</span>
        edges<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token function">Edge</span><span class="token punctuation">(</span>from<span class="token punctuation">,</span> to<span class="token punctuation">,</span> cap<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//魔改蔡队模板</span>
        edges<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token function">Edge</span><span class="token punctuation">(</span>to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//反向弧</span>
        <span class="token keyword">this</span><span class="token operator">-></span>m <span class="token operator">=</span> edges<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        G<span class="token punctuation">[</span>from<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>m <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        G<span class="token punctuation">[</span>to<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
	<span class="token keyword">bool</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">memset</span><span class="token punctuation">(</span>d<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span>
		q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
		d<span class="token punctuation">[</span>s<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		vis<span class="token punctuation">[</span>s<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> x <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				Edge<span class="token operator">&amp;</span> e <span class="token operator">=</span> edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> e<span class="token punctuation">.</span>cap <span class="token operator">></span> e<span class="token punctuation">.</span>flow<span class="token punctuation">)</span>
				<span class="token punctuation">&#123;</span>
					vis<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
					d<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span> <span class="token operator">=</span> d<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
					q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>to<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> vis<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">DFS</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> a<span class="token punctuation">)</span><span class="token comment">//x为当前点,a为当前边上流量</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">==</span> t <span class="token operator">||</span> a <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">//到达目标/流量为0</span>
			<span class="token keyword">return</span> a<span class="token punctuation">;</span>
		<span class="token keyword">int</span> flow <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> f<span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token operator">&amp;</span> i <span class="token operator">=</span> cur<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span> <span class="token comment">//从上次考虑的弧</span>
			Edge<span class="token operator">&amp;</span> e <span class="token operator">=</span> edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>d<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">==</span> d<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>f <span class="token operator">=</span> <span class="token function">DFS</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>to<span class="token punctuation">,</span> <span class="token function">min</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> e<span class="token punctuation">.</span>cap <span class="token operator">-</span> e<span class="token punctuation">.</span>flow<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">!=</span>s<span class="token punctuation">)</span><span class="token comment">//为了输出路径，添加此语句</span>
				<span class="token punctuation">&#123;</span>
					nex<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token operator">=</span>e<span class="token punctuation">.</span>to<span class="token punctuation">;</span><span class="token comment">//记录下一个节点,便于输出</span>
					vist<span class="token punctuation">[</span>e<span class="token punctuation">.</span>to<span class="token operator">-</span>all<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//打标记,找起点</span>
				<span class="token punctuation">&#125;</span>
				e<span class="token punctuation">.</span>flow <span class="token operator">+=</span> f<span class="token punctuation">;</span>
				edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">^</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>flow <span class="token operator">-=</span> f<span class="token punctuation">;</span>
				flow <span class="token operator">+=</span> f<span class="token punctuation">;</span>
				a <span class="token operator">-=</span> f<span class="token punctuation">;</span>
				<span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
					<span class="token keyword">break</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span><span class="token comment">//这题不能乱用炸点优化！！！</span>
		<span class="token keyword">return</span> flow<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> <span class="token function">Maxflow</span><span class="token punctuation">(</span><span class="token keyword">int</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> t<span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>s <span class="token operator">=</span> s<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token operator">-></span>t <span class="token operator">=</span> t<span class="token punctuation">;</span>
		<span class="token keyword">int</span> flow <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token function">memset</span><span class="token punctuation">(</span>cur<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			flow <span class="token operator">+=</span> <span class="token function">DFS</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>inf<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> flow<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> di<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span><span class="token comment">//给定DAG图G</span>
	<span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">;</span><span class="token comment">//n为DAG顶点数，m为边数</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>vist<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>vist<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	cin<span class="token operator">>></span>n<span class="token operator">>></span>m<span class="token punctuation">;</span>
	all<span class="token operator">=</span>n<span class="token punctuation">;</span>
	<span class="token keyword">int</span> s<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>t<span class="token operator">=</span><span class="token number">2</span><span class="token operator">*</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
	di<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//拆点，分别连接s和t</span>
	<span class="token punctuation">&#123;</span>
		di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>i<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>n<span class="token operator">+</span>i<span class="token punctuation">,</span>t<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> u<span class="token punctuation">,</span>v<span class="token punctuation">;</span>
		cin<span class="token operator">>></span>u<span class="token operator">>></span>v<span class="token punctuation">;</span>
		di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>n<span class="token operator">+</span>v<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//容量为1</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> ans<span class="token operator">=</span>di<span class="token punctuation">.</span><span class="token function">Maxflow</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//没标记的就是起点</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> now<span class="token operator">=</span>i<span class="token punctuation">;</span>
			cout<span class="token operator">&lt;&lt;</span>now<span class="token operator">&lt;&lt;</span><span class="token string">' '</span><span class="token punctuation">;</span>
			<span class="token keyword">while</span><span class="token punctuation">(</span>nex<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>nex<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">!=</span>t<span class="token punctuation">)</span>
			<span class="token punctuation">&#123;</span>
				cout<span class="token operator">&lt;&lt;</span>nex<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">-</span>n<span class="token operator">&lt;&lt;</span><span class="token string">' '</span><span class="token punctuation">;</span>
				now<span class="token operator">=</span>nex<span class="token punctuation">[</span>now<span class="token punctuation">]</span><span class="token operator">-</span>n<span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
			cout<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	cout<span class="token operator">&lt;&lt;</span>n<span class="token operator">-</span>ans<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span><span class="token comment">//最少路径数</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="P2774-方格取数问题-最大流，最小割"><a href="#P2774-方格取数问题-最大流，最小割" class="headerlink" title="P2774 方格取数问题(最大流，最小割)"></a>P2774 方格取数问题(最大流，最小割)</h5><p> m*n 个方格棋盘中，每个方格中有一个正整数。从方格中取数，使任意 2 个数所在方格没有公共边，且取出的数的总和最大。</p>
<p>思路：黑白染色，按奇偶性建立二分图，奇连s，偶连t。枚举每个奇数性质的方格，连接相邻的偶数性质的方格。跑dinic求出最小割，总值减去最小割即为答案。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
    <span class="token keyword">int</span> m<span class="token punctuation">,</span>n<span class="token punctuation">;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>m<span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//棋盘的行数和列数</span>
    <span class="token keyword">int</span> s<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>t<span class="token operator">=</span>m<span class="token operator">*</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>sum<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//sum为总价值</span>
    di<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>t<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> val<span class="token punctuation">,</span>no<span class="token punctuation">;</span>
			<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//i行j列的数值</span>
			sum<span class="token operator">+=</span>val<span class="token punctuation">;</span>
			no<span class="token operator">=</span>n<span class="token operator">*</span><span class="token punctuation">(</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span>j<span class="token punctuation">;</span><span class="token comment">//重点：一行n个</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token operator">+</span>j<span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//相邻的i+j奇偶性必定相斥</span>
				di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>no<span class="token punctuation">,</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//根据奇偶性连接</span>
			<span class="token punctuation">&#125;</span>
			<span class="token keyword">else</span><span class="token punctuation">&#123;</span>
				di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>no<span class="token punctuation">,</span>t<span class="token punctuation">,</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token comment">//s与t的边插入结束</span>
	<span class="token keyword">int</span> nx<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span class="token comment">//只枚举两个方向是不够的,因为是有向图</span>
	<span class="token keyword">int</span> ny<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//m行</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//n列</span>
		<span class="token punctuation">&#123;</span>
			<span class="token keyword">int</span> no<span class="token operator">=</span>n<span class="token operator">*</span><span class="token punctuation">(</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span>j<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token operator">+</span>j<span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//二分图左边的节点,开始连接右面的节点</span>
				<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> k<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>k<span class="token operator">&lt;</span><span class="token number">4</span><span class="token punctuation">;</span>k<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//枚举方向,相邻的四个方格</span>
				<span class="token punctuation">&#123;</span>
					<span class="token keyword">int</span> x<span class="token operator">=</span>i<span class="token operator">+</span>nx<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">,</span>y<span class="token operator">=</span>j<span class="token operator">+</span>ny<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//x与m轴同方向</span>
					<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">&lt;=</span><span class="token number">0</span><span class="token operator">||</span>x<span class="token operator">></span>m<span class="token operator">||</span>y<span class="token operator">&lt;=</span><span class="token number">0</span><span class="token operator">||</span>y<span class="token operator">></span>n<span class="token punctuation">)</span>
						<span class="token keyword">continue</span><span class="token punctuation">;</span>
					<span class="token keyword">int</span> no2<span class="token operator">=</span>n<span class="token operator">*</span><span class="token punctuation">(</span>x<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span>y<span class="token punctuation">;</span><span class="token comment">//相邻的方格编号</span>
					di<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>no<span class="token punctuation">,</span>no2<span class="token punctuation">,</span>inf<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span>sum<span class="token operator">-</span>di<span class="token punctuation">.</span><span class="token function">Maxflow</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>t<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="费用流"><a href="#费用流" class="headerlink" title="费用流"></a>费用流</h4><h5 id="P4012-深海机器人问题（物品在网格上）"><a href="#P4012-深海机器人问题（物品在网格上）" class="headerlink" title="P4012 深海机器人问题（物品在网格上）"></a>P4012 深海机器人问题（物品在网格上）</h5><p>物品在网格的边上，每条网格的边有权值，交叉点作为节点，在图上添加点和点之间的边，设容量为1，花费为-cost。</p>
<p>注意设置容量inf，费用为0的经过边，跑最小费用最大流，答案取负。</p>
<h5 id="P3356-火星探险问题（物品在交叉点上）"><a href="#P3356-火星探险问题（物品在交叉点上）" class="headerlink" title="P3356 火星探险问题（物品在交叉点上）"></a>P3356 火星探险问题（物品在交叉点上）</h5><p>物品在节点上，而且有障碍。</p>
<p>思路：拆点，若不为障碍，拆点间添加容量为inf，价值为0的边；若该点有所需物品，两点间额外添加一条容量为1价值为-val的边；</p>
<p>跑最小费用最大流即可，使用DFS输出路径。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		flag<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		mm<span class="token punctuation">.</span><span class="token function">dfs</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//起点(1,1),出点1,第i号机器人</span>
	<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token class-name">MCMF</span><span class="token operator">::</span><span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span><span class="token keyword">int</span> y<span class="token punctuation">,</span><span class="token keyword">int</span> u<span class="token punctuation">,</span><span class="token keyword">int</span> no<span class="token punctuation">)</span><span class="token comment">//第no号机器人到达了u节点</span>
<span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> kx<span class="token punctuation">,</span>ky<span class="token punctuation">,</span>dir<span class="token punctuation">;</span><span class="token comment">//0向下,1向右</span>
	<span class="token keyword">int</span> res<span class="token operator">=</span>p<span class="token operator">*</span>q<span class="token operator">+</span>u<span class="token punctuation">;</span><span class="token comment">//u节点的拆点编号</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>flag<span class="token punctuation">)</span><span class="token comment">//no号到达了终点</span>
		<span class="token keyword">return</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>G<span class="token punctuation">[</span>res<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>flag<span class="token punctuation">)</span>
			<span class="token keyword">return</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>res<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>flow<span class="token operator">></span><span class="token number">0</span><span class="token operator">&amp;&amp;</span><span class="token punctuation">(</span>edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>res<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>to<span class="token operator">==</span>u<span class="token operator">+</span><span class="token number">1</span><span class="token operator">||</span>edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>res<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>to<span class="token operator">==</span>u<span class="token operator">+</span>p<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//残余流量大于0</span>
		<span class="token punctuation">&#123;</span><span class="token comment">//q行,p列,x与q同方向,表示第几行</span>
			edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>res<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>flow<span class="token operator">--</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>res<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>to<span class="token operator">==</span>u<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token comment">//向右</span>
				dir<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
			<span class="token keyword">else</span>
				dir<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d %d\n"</span><span class="token punctuation">,</span>no<span class="token punctuation">,</span>dir<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>dir<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span>
                <span class="token function">dfs</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>u<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>no<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//				printf("(%d,%d)->(%d,%d)\n",x,y,x,y+1);</span>
			<span class="token keyword">else</span>
                <span class="token function">dfs</span><span class="token punctuation">(</span>x<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>y<span class="token punctuation">,</span>u<span class="token operator">+</span>p<span class="token punctuation">,</span>no<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//				printf("(%d,%d)->(%d,%d)\n",x,y,x+1,y);</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>edges<span class="token punctuation">[</span>G<span class="token punctuation">[</span>res<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">.</span>to<span class="token operator">==</span>p<span class="token operator">*</span>q<span class="token punctuation">)</span>
				flag<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="P2604-扩容费用问题"><a href="#P2604-扩容费用问题" class="headerlink" title="P2604 扩容费用问题 "></a><a target="_blank" rel="noopener" href="https://www.luogu.org/problem/P2604">P2604 扩容费用问题 </a></h5><p>问题：给出DAG每条边的容量和扩容费用，1.求出1到n的最大流。2.求出1到n的最大流增加K所需的最小扩容费用。</p>
<p>先跑一个零费用最大流。利用残余网络进行费用流扩容，将原来的0费用网络添加新的费用边，并进行<strong>限流</strong>，cost即为扩容费用。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span>m<span class="token punctuation">,</span>k<span class="token punctuation">;</span>
cin<span class="token operator">>></span>n<span class="token operator">>></span>m<span class="token operator">>></span>k<span class="token punctuation">;</span>
mm<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>n<span class="token operator">+</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	cin<span class="token operator">>></span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token operator">>></span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token operator">>></span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>c<span class="token operator">>></span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span>
	mm<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>c<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//费用为0，跑最大流</span>
<span class="token punctuation">&#125;</span>
ll cost<span class="token punctuation">;</span>
cout<span class="token operator">&lt;&lt;</span>mm<span class="token punctuation">.</span><span class="token function">MincostMaxflow</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">,</span>cost<span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span><span class="token string">' '</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>m<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">//给原来每条边上添加新边,</span>
	mm<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>u<span class="token punctuation">,</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">,</span>inf<span class="token punctuation">,</span>e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> s<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//超级源点s</span>
mm<span class="token punctuation">.</span><span class="token function">AddEdge</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>k<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//s到1进行限流</span>
mm<span class="token punctuation">.</span><span class="token function">MincostMaxflow</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>n<span class="token punctuation">,</span>cost<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//费用流</span>
cout<span class="token operator">&lt;&lt;</span>cost<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span><span class="token comment">//扩容费用</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>






  </div>
  <div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2021/06/13/howDoCSSClearul,liDefaultStyles/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">css怎么清除ul,li默认样式？ </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2021/06/13/gitIgnoreFiles/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">git之忽略文件 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%BE%E8%AE%BA"><span class="toc-text">图论</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%A6%82%E5%BF%B5"><span class="toc-text">常见概念</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E6%B5%81%E5%B8%B8%E7%94%A8%E6%A6%82%E5%BF%B5"><span class="toc-text">网络流常用概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%A6%E5%9B%BE%E5%B8%B8%E7%94%A8%E6%A6%82%E5%BF%B5"><span class="toc-text">弦图常用概念</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%BB%93%E8%AE%BA"><span class="toc-text">常见结论</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%A6%E5%9B%BE"><span class="toc-text">弦图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%8A%BF%E7%AE%97%E6%B3%95%E6%B1%82%E6%B6%88%E9%99%A4%E5%BA%8F%E5%88%97%E5%B9%B6%E5%88%A4%E5%AE%9A%E5%BC%A6%E5%9B%BE"><span class="toc-text">最大势算法求消除序列并判定弦图</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E5%85%83%E7%8E%AF"><span class="toc-text">三元环</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A5%E9%AA%A4"><span class="toc-text">步骤</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E7%9F%AD%E8%B7%AF"><span class="toc-text">最短路</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Dijkstra%E5%A0%86%E4%BC%98%E5%8C%96"><span class="toc-text">Dijkstra堆优化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SPFA"><span class="toc-text">SPFA</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#BFS%E5%AE%9E%E7%8E%B0%EF%BC%88%E6%B1%82%E6%9C%80%E7%9F%AD%E8%B7%AF%EF%BC%89"><span class="toc-text">BFS实现（求最短路）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#DFS%E4%BC%98%E5%8C%96%EF%BC%88%E8%B4%9F%E7%8E%AF%E6%A3%80%E6%B5%8B%EF%BC%89"><span class="toc-text">DFS优化（负环检测）</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Floyd"><span class="toc-text">Floyd</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%9B%A2"><span class="toc-text">最大团</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%9B%A2%E5%A4%A7%E5%B0%8F"><span class="toc-text">最大团大小</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%AE%E5%88%86%E7%BA%A6%E6%9D%9F"><span class="toc-text">差分约束</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E6%B3%95"><span class="toc-text">解法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91-MST"><span class="toc-text">最小生成树(MST)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Prim%E7%AE%97%E6%B3%95"><span class="toc-text">Prim算法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Kruskal"><span class="toc-text">Kruskal</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Boruvka"><span class="toc-text">Boruvka</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-text">代码</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B8%E5%9E%8B%E4%BE%8B%E9%A2%98"><span class="toc-text">典型例题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91"><span class="toc-text">次小生成树</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%A5%E6%A0%BC%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91-%E6%B4%9B%E8%B0%B7P4180"><span class="toc-text">严格次小生成树(洛谷P4180)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%89%E5%90%91%E5%9B%BE%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91%EF%BC%88MDST%EF%BC%8C%E6%9C%80%E5%B0%8F%E6%A0%91%E5%BD%A2%E5%9B%BE%EF%BC%89"><span class="toc-text">有向图最小生成树（MDST，最小树形图）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%B1%E5%88%98%E7%AE%97%E6%B3%95%EF%BC%88Edmonds-%E7%AE%97%E6%B3%95%EF%BC%89"><span class="toc-text">朱刘算法（Edmonds 算法）</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-1"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF"><span class="toc-text">模板</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%99%9A%E6%A0%91"><span class="toc-text">虚树</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A5%E9%AA%A4-1"><span class="toc-text">步骤</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="toc-text">代码示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8F"><span class="toc-text">拓扑排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%A5%E5%9B%BE%E6%90%9C%E7%B4%A2"><span class="toc-text">补图搜索</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%A5%E5%9B%BE%E8%BF%9E%E9%80%9A%E5%9D%97-0-1-MST"><span class="toc-text">补图连通块 0-1 MST </span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%BA%E8%BF%9E%E9%80%9A%E5%88%86%E9%87%8F"><span class="toc-text">强连通分量</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Tarjan"><span class="toc-text">Tarjan</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#Tarjan%E7%BC%A9%E7%82%B9-DAG-%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8Fdp"><span class="toc-text">Tarjan缩点+DAG 拓扑排序dp</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Tarjan%E6%97%A0%E5%90%91%E5%9B%BE%E6%B1%82%E5%89%B2%E7%82%B9"><span class="toc-text">Tarjan无向图求割点</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%97%A0%E5%90%91%E5%9B%BE%E6%B1%82%E6%A1%A5"><span class="toc-text">无向图求桥</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-SAT%E9%97%AE%E9%A2%98"><span class="toc-text">2-SAT问题</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-2"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BB%BA%E5%9B%BE%E6%96%B9%E5%BC%8F"><span class="toc-text">建图方式</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88-LCA"><span class="toc-text">最近公共祖先(LCA)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%80%8D%E5%A2%9E"><span class="toc-text">倍增</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BA%94%E7%94%A8"><span class="toc-text">应用</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E4%B8%8A%E5%B7%AE%E5%88%86"><span class="toc-text">树上差分</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%A7%E8%B4%A8"><span class="toc-text">性质</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%82%B9%E7%9A%84%E5%B7%AE%E5%88%86"><span class="toc-text">点的差分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C"><span class="toc-text">操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9"><span class="toc-text">修改</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BE%B9%E7%9A%84%E5%B7%AE%E5%88%86"><span class="toc-text">边的差分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C-1"><span class="toc-text">操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9-1"><span class="toc-text">修改</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2"><span class="toc-text">查询</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86"><span class="toc-text">树链剖分</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E7%94%A8%E6%A8%A1%E6%9D%BF"><span class="toc-text">通用模板</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#P3384-%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86"><span class="toc-text">P3384 树链剖分 </span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%82%B9%E5%88%86%E6%B2%BB"><span class="toc-text">点分治</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%91%E7%9A%84%E9%87%8D%E5%BF%83"><span class="toc-text">树的重心</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-3"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BE%8B%E9%A2%98"><span class="toc-text">例题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E4%B8%8A%E5%90%AF%E5%8F%91%E5%BC%8F%E5%90%88%E5%B9%B6%EF%BC%88DSU-on-tree%EF%BC%89"><span class="toc-text">树上启发式合并（DSU on tree）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-4"><span class="toc-text">流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E5%88%86%E5%9B%BE"><span class="toc-text">二分图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89-1"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E4%BA%8C%E5%88%86%E5%9B%BE%E7%BB%93%E8%AE%BA"><span class="toc-text">常见二分图结论</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%82%E7%94%A8%E4%BB%BB%E6%84%8F%E5%9B%BE%E7%9A%84%E7%BB%93%E8%AE%BA"><span class="toc-text">适用任意图的结论</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E5%88%86%E5%9B%BE%E5%88%A4%E5%AE%9A%EF%BC%88%E9%BB%91%E7%99%BD%E6%9F%93%E8%89%B2%E6%B3%95%EF%BC%89"><span class="toc-text">二分图判定（黑白染色法）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8C%88%E7%89%99%E5%88%A9%E7%AE%97%E6%B3%95"><span class="toc-text">匈牙利算法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%82%BB%E6%8E%A5%E8%A1%A8"><span class="toc-text">邻接表</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#KM%E7%AE%97%E6%B3%95"><span class="toc-text">KM算法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#BFS%EF%BC%8C%E5%A4%8D%E6%9D%82%E5%BA%A6%E7%A8%B3%E7%9A%84%E4%B8%80%E6%89%B9"><span class="toc-text">BFS，复杂度稳的一批</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B7%A6%E5%8F%B3%E6%95%B0%E7%9B%AE%E4%B8%8D%E7%AD%89%E7%9A%84%E6%A8%A1%E6%9D%BF"><span class="toc-text">左右数目不等的模板</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#CSL%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">CSL的写法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E6%B5%81"><span class="toc-text">网络流</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF%E9%83%A8%E5%88%86"><span class="toc-text">模板部分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#dinic"><span class="toc-text">dinic</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dijkstra%E8%B4%B9%E7%94%A8%E6%B5%81"><span class="toc-text">dijkstra费用流</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E6%B5%81"><span class="toc-text">最大流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%AA%E7%A9%BA%E9%A3%9E%E8%A1%8C%E8%AE%A1%E5%88%92%E9%97%AE%E9%A2%98%EF%BC%88%E6%9C%80%E5%B0%8F%E5%89%B2-%E6%94%B6%E7%9B%8A%E6%9C%80%E5%A4%A7%E9%97%AE%E9%A2%98%EF%BC%89"><span class="toc-text">太空飞行计划问题（最小割,收益最大问题）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9C%80%E5%B0%8F%E8%B7%AF%E5%BE%84%E8%A6%86%E7%9B%96%E9%97%AE%E9%A2%98-%E6%9C%80%E5%A4%A7%E6%B5%81-%E6%9C%80%E5%B0%8F%E4%B8%8D%E7%9B%B8%E4%BA%A4%E8%B7%AF%E5%BE%84%E8%A6%86%E7%9B%96%E6%A8%A1%E5%9E%8B-%E8%A6%81%E6%B1%82%E8%B7%AF%E5%BE%84%E6%95%B0%E6%9C%80%E5%B0%91-%E8%B7%AF%E5%BE%84%E8%BE%93%E5%87%BA"><span class="toc-text">最小路径覆盖问题(最大流,最小不相交路径覆盖模型,要求路径数最少,路径输出)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P2774-%E6%96%B9%E6%A0%BC%E5%8F%96%E6%95%B0%E9%97%AE%E9%A2%98-%E6%9C%80%E5%A4%A7%E6%B5%81%EF%BC%8C%E6%9C%80%E5%B0%8F%E5%89%B2"><span class="toc-text">P2774 方格取数问题(最大流，最小割)</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B4%B9%E7%94%A8%E6%B5%81"><span class="toc-text">费用流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#P4012-%E6%B7%B1%E6%B5%B7%E6%9C%BA%E5%99%A8%E4%BA%BA%E9%97%AE%E9%A2%98%EF%BC%88%E7%89%A9%E5%93%81%E5%9C%A8%E7%BD%91%E6%A0%BC%E4%B8%8A%EF%BC%89"><span class="toc-text">P4012 深海机器人问题（物品在网格上）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P3356-%E7%81%AB%E6%98%9F%E6%8E%A2%E9%99%A9%E9%97%AE%E9%A2%98%EF%BC%88%E7%89%A9%E5%93%81%E5%9C%A8%E4%BA%A4%E5%8F%89%E7%82%B9%E4%B8%8A%EF%BC%89"><span class="toc-text">P3356 火星探险问题（物品在交叉点上）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P2604-%E6%89%A9%E5%AE%B9%E8%B4%B9%E7%94%A8%E9%97%AE%E9%A2%98"><span class="toc-text">P2604 扩容费用问题 </span></a></li></ol></li></ol></li></ol></li></ol>
</div></main>
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg" 
    class="author-img" 
    alt="author avatar">

<p class="author-name">霜序廿</p>
<p class="author-description">无限进步</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>253</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>6</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>51</span>
    <span>标签</span>
  </a>
</div>

  <div class="author-card-society">
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://github.com/shuangxunian">
          <i class="iconfont icon-github society-icon"></i>
        </a>
      </div>
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://space.bilibili.com/391117803">
          <i class="iconfont icon-bilibili society-icon"></i>
        </a>
      </div>
    
  </div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%BE%E8%AE%BA"><span class="toc-text">图论</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%A6%82%E5%BF%B5"><span class="toc-text">常见概念</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E6%B5%81%E5%B8%B8%E7%94%A8%E6%A6%82%E5%BF%B5"><span class="toc-text">网络流常用概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%A6%E5%9B%BE%E5%B8%B8%E7%94%A8%E6%A6%82%E5%BF%B5"><span class="toc-text">弦图常用概念</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%BB%93%E8%AE%BA"><span class="toc-text">常见结论</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%A6%E5%9B%BE"><span class="toc-text">弦图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%8A%BF%E7%AE%97%E6%B3%95%E6%B1%82%E6%B6%88%E9%99%A4%E5%BA%8F%E5%88%97%E5%B9%B6%E5%88%A4%E5%AE%9A%E5%BC%A6%E5%9B%BE"><span class="toc-text">最大势算法求消除序列并判定弦图</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E5%85%83%E7%8E%AF"><span class="toc-text">三元环</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A5%E9%AA%A4"><span class="toc-text">步骤</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E7%9F%AD%E8%B7%AF"><span class="toc-text">最短路</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Dijkstra%E5%A0%86%E4%BC%98%E5%8C%96"><span class="toc-text">Dijkstra堆优化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SPFA"><span class="toc-text">SPFA</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#BFS%E5%AE%9E%E7%8E%B0%EF%BC%88%E6%B1%82%E6%9C%80%E7%9F%AD%E8%B7%AF%EF%BC%89"><span class="toc-text">BFS实现（求最短路）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#DFS%E4%BC%98%E5%8C%96%EF%BC%88%E8%B4%9F%E7%8E%AF%E6%A3%80%E6%B5%8B%EF%BC%89"><span class="toc-text">DFS优化（负环检测）</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Floyd"><span class="toc-text">Floyd</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%9B%A2"><span class="toc-text">最大团</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%9B%A2%E5%A4%A7%E5%B0%8F"><span class="toc-text">最大团大小</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%AE%E5%88%86%E7%BA%A6%E6%9D%9F"><span class="toc-text">差分约束</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E6%B3%95"><span class="toc-text">解法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91-MST"><span class="toc-text">最小生成树(MST)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Prim%E7%AE%97%E6%B3%95"><span class="toc-text">Prim算法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Kruskal"><span class="toc-text">Kruskal</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Boruvka"><span class="toc-text">Boruvka</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-text">代码</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B8%E5%9E%8B%E4%BE%8B%E9%A2%98"><span class="toc-text">典型例题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91"><span class="toc-text">次小生成树</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%A5%E6%A0%BC%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91-%E6%B4%9B%E8%B0%B7P4180"><span class="toc-text">严格次小生成树(洛谷P4180)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%89%E5%90%91%E5%9B%BE%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91%EF%BC%88MDST%EF%BC%8C%E6%9C%80%E5%B0%8F%E6%A0%91%E5%BD%A2%E5%9B%BE%EF%BC%89"><span class="toc-text">有向图最小生成树（MDST，最小树形图）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%B1%E5%88%98%E7%AE%97%E6%B3%95%EF%BC%88Edmonds-%E7%AE%97%E6%B3%95%EF%BC%89"><span class="toc-text">朱刘算法（Edmonds 算法）</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-1"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF"><span class="toc-text">模板</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%99%9A%E6%A0%91"><span class="toc-text">虚树</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A5%E9%AA%A4-1"><span class="toc-text">步骤</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="toc-text">代码示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8F"><span class="toc-text">拓扑排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%A5%E5%9B%BE%E6%90%9C%E7%B4%A2"><span class="toc-text">补图搜索</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%A5%E5%9B%BE%E8%BF%9E%E9%80%9A%E5%9D%97-0-1-MST"><span class="toc-text">补图连通块 0-1 MST </span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%BA%E8%BF%9E%E9%80%9A%E5%88%86%E9%87%8F"><span class="toc-text">强连通分量</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Tarjan"><span class="toc-text">Tarjan</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#Tarjan%E7%BC%A9%E7%82%B9-DAG-%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8Fdp"><span class="toc-text">Tarjan缩点+DAG 拓扑排序dp</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Tarjan%E6%97%A0%E5%90%91%E5%9B%BE%E6%B1%82%E5%89%B2%E7%82%B9"><span class="toc-text">Tarjan无向图求割点</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%97%A0%E5%90%91%E5%9B%BE%E6%B1%82%E6%A1%A5"><span class="toc-text">无向图求桥</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-SAT%E9%97%AE%E9%A2%98"><span class="toc-text">2-SAT问题</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-2"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BB%BA%E5%9B%BE%E6%96%B9%E5%BC%8F"><span class="toc-text">建图方式</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88-LCA"><span class="toc-text">最近公共祖先(LCA)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%80%8D%E5%A2%9E"><span class="toc-text">倍增</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BA%94%E7%94%A8"><span class="toc-text">应用</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E4%B8%8A%E5%B7%AE%E5%88%86"><span class="toc-text">树上差分</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%A7%E8%B4%A8"><span class="toc-text">性质</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%82%B9%E7%9A%84%E5%B7%AE%E5%88%86"><span class="toc-text">点的差分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C"><span class="toc-text">操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9"><span class="toc-text">修改</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BE%B9%E7%9A%84%E5%B7%AE%E5%88%86"><span class="toc-text">边的差分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C-1"><span class="toc-text">操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9-1"><span class="toc-text">修改</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2"><span class="toc-text">查询</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86"><span class="toc-text">树链剖分</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E7%94%A8%E6%A8%A1%E6%9D%BF"><span class="toc-text">通用模板</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#P3384-%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86"><span class="toc-text">P3384 树链剖分 </span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%82%B9%E5%88%86%E6%B2%BB"><span class="toc-text">点分治</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%91%E7%9A%84%E9%87%8D%E5%BF%83"><span class="toc-text">树的重心</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-3"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BE%8B%E9%A2%98"><span class="toc-text">例题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E4%B8%8A%E5%90%AF%E5%8F%91%E5%BC%8F%E5%90%88%E5%B9%B6%EF%BC%88DSU-on-tree%EF%BC%89"><span class="toc-text">树上启发式合并（DSU on tree）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-4"><span class="toc-text">流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E5%88%86%E5%9B%BE"><span class="toc-text">二分图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89-1"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E4%BA%8C%E5%88%86%E5%9B%BE%E7%BB%93%E8%AE%BA"><span class="toc-text">常见二分图结论</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%82%E7%94%A8%E4%BB%BB%E6%84%8F%E5%9B%BE%E7%9A%84%E7%BB%93%E8%AE%BA"><span class="toc-text">适用任意图的结论</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E5%88%86%E5%9B%BE%E5%88%A4%E5%AE%9A%EF%BC%88%E9%BB%91%E7%99%BD%E6%9F%93%E8%89%B2%E6%B3%95%EF%BC%89"><span class="toc-text">二分图判定（黑白染色法）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8C%88%E7%89%99%E5%88%A9%E7%AE%97%E6%B3%95"><span class="toc-text">匈牙利算法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%82%BB%E6%8E%A5%E8%A1%A8"><span class="toc-text">邻接表</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#KM%E7%AE%97%E6%B3%95"><span class="toc-text">KM算法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#BFS%EF%BC%8C%E5%A4%8D%E6%9D%82%E5%BA%A6%E7%A8%B3%E7%9A%84%E4%B8%80%E6%89%B9"><span class="toc-text">BFS，复杂度稳的一批</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B7%A6%E5%8F%B3%E6%95%B0%E7%9B%AE%E4%B8%8D%E7%AD%89%E7%9A%84%E6%A8%A1%E6%9D%BF"><span class="toc-text">左右数目不等的模板</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#CSL%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">CSL的写法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E6%B5%81"><span class="toc-text">网络流</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF%E9%83%A8%E5%88%86"><span class="toc-text">模板部分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#dinic"><span class="toc-text">dinic</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dijkstra%E8%B4%B9%E7%94%A8%E6%B5%81"><span class="toc-text">dijkstra费用流</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E6%B5%81"><span class="toc-text">最大流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%AA%E7%A9%BA%E9%A3%9E%E8%A1%8C%E8%AE%A1%E5%88%92%E9%97%AE%E9%A2%98%EF%BC%88%E6%9C%80%E5%B0%8F%E5%89%B2-%E6%94%B6%E7%9B%8A%E6%9C%80%E5%A4%A7%E9%97%AE%E9%A2%98%EF%BC%89"><span class="toc-text">太空飞行计划问题（最小割,收益最大问题）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9C%80%E5%B0%8F%E8%B7%AF%E5%BE%84%E8%A6%86%E7%9B%96%E9%97%AE%E9%A2%98-%E6%9C%80%E5%A4%A7%E6%B5%81-%E6%9C%80%E5%B0%8F%E4%B8%8D%E7%9B%B8%E4%BA%A4%E8%B7%AF%E5%BE%84%E8%A6%86%E7%9B%96%E6%A8%A1%E5%9E%8B-%E8%A6%81%E6%B1%82%E8%B7%AF%E5%BE%84%E6%95%B0%E6%9C%80%E5%B0%91-%E8%B7%AF%E5%BE%84%E8%BE%93%E5%87%BA"><span class="toc-text">最小路径覆盖问题(最大流,最小不相交路径覆盖模型,要求路径数最少,路径输出)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P2774-%E6%96%B9%E6%A0%BC%E5%8F%96%E6%95%B0%E9%97%AE%E9%A2%98-%E6%9C%80%E5%A4%A7%E6%B5%81%EF%BC%8C%E6%9C%80%E5%B0%8F%E5%89%B2"><span class="toc-text">P2774 方格取数问题(最大流，最小割)</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B4%B9%E7%94%A8%E6%B5%81"><span class="toc-text">费用流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#P4012-%E6%B7%B1%E6%B5%B7%E6%9C%BA%E5%99%A8%E4%BA%BA%E9%97%AE%E9%A2%98%EF%BC%88%E7%89%A9%E5%93%81%E5%9C%A8%E7%BD%91%E6%A0%BC%E4%B8%8A%EF%BC%89"><span class="toc-text">P4012 深海机器人问题（物品在网格上）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P3356-%E7%81%AB%E6%98%9F%E6%8E%A2%E9%99%A9%E9%97%AE%E9%A2%98%EF%BC%88%E7%89%A9%E5%93%81%E5%9C%A8%E4%BA%A4%E5%8F%89%E7%82%B9%E4%B8%8A%EF%BC%89"><span class="toc-text">P3356 火星探险问题（物品在交叉点上）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P2604-%E6%89%A9%E5%AE%B9%E8%B4%B9%E7%94%A8%E9%97%AE%E9%A2%98"><span class="toc-text">P2604 扩容费用问题 </span></a></li></ol></li></ol></li></ol></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/">
        <div class="categories-list-item">
          技术文章
          <span class="categories-list-item-badge">218</span>
        </div>
      </a>
    
      <a href="/categories/%E5%85%B6%E4%BB%96/">
        <div class="categories-list-item">
          其他
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/%E6%97%85%E6%B8%B8/">
        <div class="categories-list-item">
          旅游
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/%E7%AE%97%E6%B3%95/">
        <div class="categories-list-item">
          算法
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
      <a href="/categories/%E8%80%83%E8%AF%95/">
        <div class="categories-list-item">
          考试
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
      <a href="/categories/%E9%98%85%E8%AF%BB/">
        <div class="categories-list-item">
          阅读
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/js/" 
        title="js">
        <div class="tags-list-item">js</div>
      </a>
    
      <a 
        href="/tags/vue/" 
        title="vue">
        <div class="tags-list-item">vue</div>
      </a>
    
      <a 
        href="/tags/%E9%9D%A2%E8%AF%95/" 
        title="面试">
        <div class="tags-list-item">面试</div>
      </a>
    
      <a 
        href="/tags/css/" 
        title="css">
        <div class="tags-list-item">css</div>
      </a>
    
      <a 
        href="/tags/%E7%BD%91%E7%BB%9C/" 
        title="网络">
        <div class="tags-list-item">网络</div>
      </a>
    
      <a 
        href="/tags/%E5%85%B6%E4%BB%96/" 
        title="其他">
        <div class="tags-list-item">其他</div>
      </a>
    
      <a 
        href="/tags/%E7%AE%97%E6%B3%95/" 
        title="算法">
        <div class="tags-list-item">算法</div>
      </a>
    
      <a 
        href="/tags/%E6%B5%8F%E8%A7%88%E5%99%A8/" 
        title="浏览器">
        <div class="tags-list-item">浏览器</div>
      </a>
    
      <a 
        href="/tags/html/" 
        title="html">
        <div class="tags-list-item">html</div>
      </a>
    
      <a 
        href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" 
        title="操作系统">
        <div class="tags-list-item">操作系统</div>
      </a>
    
      <a 
        href="/tags/%E8%80%83%E8%AF%95/" 
        title="考试">
        <div class="tags-list-item">考试</div>
      </a>
    
      <a 
        href="/tags/%E8%BD%AF%E5%AE%9E%E5%8A%9B/" 
        title="软实力">
        <div class="tags-list-item">软实力</div>
      </a>
    
      <a 
        href="/tags/DOM/" 
        title="DOM">
        <div class="tags-list-item">DOM</div>
      </a>
    
      <a 
        href="/tags/%E8%BD%AE%E5%AD%90/" 
        title="轮子">
        <div class="tags-list-item">轮子</div>
      </a>
    
      <a 
        href="/tags/%E7%BD%91%E7%BB%9C%E5%8E%9F%E7%90%86/" 
        title="网络原理">
        <div class="tags-list-item">网络原理</div>
      </a>
    
      <a 
        href="/tags/debug/" 
        title="debug">
        <div class="tags-list-item">debug</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%BE%E8%AE%BA"><span class="toc-text">图论</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%A6%82%E5%BF%B5"><span class="toc-text">常见概念</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E6%B5%81%E5%B8%B8%E7%94%A8%E6%A6%82%E5%BF%B5"><span class="toc-text">网络流常用概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%A6%E5%9B%BE%E5%B8%B8%E7%94%A8%E6%A6%82%E5%BF%B5"><span class="toc-text">弦图常用概念</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%BB%93%E8%AE%BA"><span class="toc-text">常见结论</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%A6%E5%9B%BE"><span class="toc-text">弦图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%8A%BF%E7%AE%97%E6%B3%95%E6%B1%82%E6%B6%88%E9%99%A4%E5%BA%8F%E5%88%97%E5%B9%B6%E5%88%A4%E5%AE%9A%E5%BC%A6%E5%9B%BE"><span class="toc-text">最大势算法求消除序列并判定弦图</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E5%85%83%E7%8E%AF"><span class="toc-text">三元环</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A5%E9%AA%A4"><span class="toc-text">步骤</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E7%9F%AD%E8%B7%AF"><span class="toc-text">最短路</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Dijkstra%E5%A0%86%E4%BC%98%E5%8C%96"><span class="toc-text">Dijkstra堆优化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SPFA"><span class="toc-text">SPFA</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#BFS%E5%AE%9E%E7%8E%B0%EF%BC%88%E6%B1%82%E6%9C%80%E7%9F%AD%E8%B7%AF%EF%BC%89"><span class="toc-text">BFS实现（求最短路）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#DFS%E4%BC%98%E5%8C%96%EF%BC%88%E8%B4%9F%E7%8E%AF%E6%A3%80%E6%B5%8B%EF%BC%89"><span class="toc-text">DFS优化（负环检测）</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Floyd"><span class="toc-text">Floyd</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%9B%A2"><span class="toc-text">最大团</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E5%9B%A2%E5%A4%A7%E5%B0%8F"><span class="toc-text">最大团大小</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%AE%E5%88%86%E7%BA%A6%E6%9D%9F"><span class="toc-text">差分约束</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E6%B3%95"><span class="toc-text">解法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91-MST"><span class="toc-text">最小生成树(MST)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Prim%E7%AE%97%E6%B3%95"><span class="toc-text">Prim算法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Kruskal"><span class="toc-text">Kruskal</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Boruvka"><span class="toc-text">Boruvka</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-text">代码</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B8%E5%9E%8B%E4%BE%8B%E9%A2%98"><span class="toc-text">典型例题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91"><span class="toc-text">次小生成树</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%A5%E6%A0%BC%E6%AC%A1%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91-%E6%B4%9B%E8%B0%B7P4180"><span class="toc-text">严格次小生成树(洛谷P4180)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%89%E5%90%91%E5%9B%BE%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91%EF%BC%88MDST%EF%BC%8C%E6%9C%80%E5%B0%8F%E6%A0%91%E5%BD%A2%E5%9B%BE%EF%BC%89"><span class="toc-text">有向图最小生成树（MDST，最小树形图）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%B1%E5%88%98%E7%AE%97%E6%B3%95%EF%BC%88Edmonds-%E7%AE%97%E6%B3%95%EF%BC%89"><span class="toc-text">朱刘算法（Edmonds 算法）</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-1"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF"><span class="toc-text">模板</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%99%9A%E6%A0%91"><span class="toc-text">虚树</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A5%E9%AA%A4-1"><span class="toc-text">步骤</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="toc-text">代码示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8F"><span class="toc-text">拓扑排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%A5%E5%9B%BE%E6%90%9C%E7%B4%A2"><span class="toc-text">补图搜索</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%A5%E5%9B%BE%E8%BF%9E%E9%80%9A%E5%9D%97-0-1-MST"><span class="toc-text">补图连通块 0-1 MST </span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%BA%E8%BF%9E%E9%80%9A%E5%88%86%E9%87%8F"><span class="toc-text">强连通分量</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Tarjan"><span class="toc-text">Tarjan</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#Tarjan%E7%BC%A9%E7%82%B9-DAG-%E6%8B%93%E6%89%91%E6%8E%92%E5%BA%8Fdp"><span class="toc-text">Tarjan缩点+DAG 拓扑排序dp</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Tarjan%E6%97%A0%E5%90%91%E5%9B%BE%E6%B1%82%E5%89%B2%E7%82%B9"><span class="toc-text">Tarjan无向图求割点</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%97%A0%E5%90%91%E5%9B%BE%E6%B1%82%E6%A1%A5"><span class="toc-text">无向图求桥</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-SAT%E9%97%AE%E9%A2%98"><span class="toc-text">2-SAT问题</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-2"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BB%BA%E5%9B%BE%E6%96%B9%E5%BC%8F"><span class="toc-text">建图方式</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88-LCA"><span class="toc-text">最近公共祖先(LCA)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%80%8D%E5%A2%9E"><span class="toc-text">倍增</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BA%94%E7%94%A8"><span class="toc-text">应用</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E4%B8%8A%E5%B7%AE%E5%88%86"><span class="toc-text">树上差分</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%A7%E8%B4%A8"><span class="toc-text">性质</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%82%B9%E7%9A%84%E5%B7%AE%E5%88%86"><span class="toc-text">点的差分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C"><span class="toc-text">操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9"><span class="toc-text">修改</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BE%B9%E7%9A%84%E5%B7%AE%E5%88%86"><span class="toc-text">边的差分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C-1"><span class="toc-text">操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9-1"><span class="toc-text">修改</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2"><span class="toc-text">查询</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86"><span class="toc-text">树链剖分</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E7%94%A8%E6%A8%A1%E6%9D%BF"><span class="toc-text">通用模板</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#P3384-%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86"><span class="toc-text">P3384 树链剖分 </span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%82%B9%E5%88%86%E6%B2%BB"><span class="toc-text">点分治</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%91%E7%9A%84%E9%87%8D%E5%BF%83"><span class="toc-text">树的重心</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-3"><span class="toc-text">流程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BE%8B%E9%A2%98"><span class="toc-text">例题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%91%E4%B8%8A%E5%90%AF%E5%8F%91%E5%BC%8F%E5%90%88%E5%B9%B6%EF%BC%88DSU-on-tree%EF%BC%89"><span class="toc-text">树上启发式合并（DSU on tree）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B-4"><span class="toc-text">流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E5%88%86%E5%9B%BE"><span class="toc-text">二分图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89-1"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E4%BA%8C%E5%88%86%E5%9B%BE%E7%BB%93%E8%AE%BA"><span class="toc-text">常见二分图结论</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%82%E7%94%A8%E4%BB%BB%E6%84%8F%E5%9B%BE%E7%9A%84%E7%BB%93%E8%AE%BA"><span class="toc-text">适用任意图的结论</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E5%88%86%E5%9B%BE%E5%88%A4%E5%AE%9A%EF%BC%88%E9%BB%91%E7%99%BD%E6%9F%93%E8%89%B2%E6%B3%95%EF%BC%89"><span class="toc-text">二分图判定（黑白染色法）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8C%88%E7%89%99%E5%88%A9%E7%AE%97%E6%B3%95"><span class="toc-text">匈牙利算法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%82%BB%E6%8E%A5%E8%A1%A8"><span class="toc-text">邻接表</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#KM%E7%AE%97%E6%B3%95"><span class="toc-text">KM算法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#BFS%EF%BC%8C%E5%A4%8D%E6%9D%82%E5%BA%A6%E7%A8%B3%E7%9A%84%E4%B8%80%E6%89%B9"><span class="toc-text">BFS，复杂度稳的一批</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B7%A6%E5%8F%B3%E6%95%B0%E7%9B%AE%E4%B8%8D%E7%AD%89%E7%9A%84%E6%A8%A1%E6%9D%BF"><span class="toc-text">左右数目不等的模板</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#CSL%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">CSL的写法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E6%B5%81"><span class="toc-text">网络流</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF%E9%83%A8%E5%88%86"><span class="toc-text">模板部分</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#dinic"><span class="toc-text">dinic</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dijkstra%E8%B4%B9%E7%94%A8%E6%B5%81"><span class="toc-text">dijkstra费用流</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E5%A4%A7%E6%B5%81"><span class="toc-text">最大流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%AA%E7%A9%BA%E9%A3%9E%E8%A1%8C%E8%AE%A1%E5%88%92%E9%97%AE%E9%A2%98%EF%BC%88%E6%9C%80%E5%B0%8F%E5%89%B2-%E6%94%B6%E7%9B%8A%E6%9C%80%E5%A4%A7%E9%97%AE%E9%A2%98%EF%BC%89"><span class="toc-text">太空飞行计划问题（最小割,收益最大问题）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9C%80%E5%B0%8F%E8%B7%AF%E5%BE%84%E8%A6%86%E7%9B%96%E9%97%AE%E9%A2%98-%E6%9C%80%E5%A4%A7%E6%B5%81-%E6%9C%80%E5%B0%8F%E4%B8%8D%E7%9B%B8%E4%BA%A4%E8%B7%AF%E5%BE%84%E8%A6%86%E7%9B%96%E6%A8%A1%E5%9E%8B-%E8%A6%81%E6%B1%82%E8%B7%AF%E5%BE%84%E6%95%B0%E6%9C%80%E5%B0%91-%E8%B7%AF%E5%BE%84%E8%BE%93%E5%87%BA"><span class="toc-text">最小路径覆盖问题(最大流,最小不相交路径覆盖模型,要求路径数最少,路径输出)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P2774-%E6%96%B9%E6%A0%BC%E5%8F%96%E6%95%B0%E9%97%AE%E9%A2%98-%E6%9C%80%E5%A4%A7%E6%B5%81%EF%BC%8C%E6%9C%80%E5%B0%8F%E5%89%B2"><span class="toc-text">P2774 方格取数问题(最大流，最小割)</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B4%B9%E7%94%A8%E6%B5%81"><span class="toc-text">费用流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#P4012-%E6%B7%B1%E6%B5%B7%E6%9C%BA%E5%99%A8%E4%BA%BA%E9%97%AE%E9%A2%98%EF%BC%88%E7%89%A9%E5%93%81%E5%9C%A8%E7%BD%91%E6%A0%BC%E4%B8%8A%EF%BC%89"><span class="toc-text">P4012 深海机器人问题（物品在网格上）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P3356-%E7%81%AB%E6%98%9F%E6%8E%A2%E9%99%A9%E9%97%AE%E9%A2%98%EF%BC%88%E7%89%A9%E5%93%81%E5%9C%A8%E4%BA%A4%E5%8F%89%E7%82%B9%E4%B8%8A%EF%BC%89"><span class="toc-text">P3356 火星探险问题（物品在交叉点上）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#P2604-%E6%89%A9%E5%AE%B9%E8%B4%B9%E7%94%A8%E9%97%AE%E9%A2%98"><span class="toc-text">P2604 扩容费用问题 </span></a></li></ol></li></ol></li></ol></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-05-01</div>
        <a href="/2022/05/01/typescriptHome/"><div class="recent-posts-item-content">typescript</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptTheVariousWaysAndAdvantagesAndDisadvantagesOfDeepInheritance/"><div class="recent-posts-item-content">JavaScript深入之继承的多种方式和优缺点</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptGoFromPrototypeToPrototypeChain/"><div class="recent-posts-item-content">JavaScript深入之从原型到原型链</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptMemoryLeakTutorial/"><div class="recent-posts-item-content">JavaScript 内存泄漏教程</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    关注嘉然！顿顿解馋！
  </div>
  <div class="recent-posts-list">
    
      <img 
        src="https://api2.mubu.com/v3/document_image/2697c6ae-10ee-41a3-9099-304bdb252d31-3807603.jpg" 
        class="myadd-img" 
        alt="author avatar">
    
  </div>
</div>
  </article>
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2020 -
          
          2022
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          霜序廿的个人网站
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer> 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton" 
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
     
    
    
  </body>
</html>
